本篇是DQL的条件查询篇,主要学习的内容如下所示:(1)条件查询基本语法格式;(2)等于(=);(3)不等于(<>或者!=);(4)大于(>);(5)逻辑查询运算符;(6)并且(AND);(7)或者(OR);(8)模糊查询(like);(9)区间查询(between…and);(10)IN查询;(11)NOT IN查询;(12)NULL的注意事项;(13)IS NULL查询;(14)IS NOT NULL查询;(15)安全等于(<=>);(16)经典面试题;(17)文章小结等。
mysql> select * from envy5 where a >1; +------+-------+ | a | b | +------+-------+ | 2 | hello | +------+-------+ 1 row in set (0.00 sec)
mysql> select * from envy5 where b > 'a'; +------+-------+ | a | b | +------+-------+ | 1 | abc | | 2 | hello | +------+-------+ 2 rows in set (0.00 sec)
mysql> select * from envy5 where b > 'ac'; +------+-------+ | a | b | +------+-------+ | 2 | hello | +------+-------+ 1 row in set (0.00 sec)
mysql> select * from product p where p.name like '%水%'; +----+-------+--------------+ | id | price | name | +----+-------+--------------+ | 1 | 10 | 矿泉水 | | 3 | 30 | 补水面膜 | | 5 | 50 | 王水 | +----+-------+--------------+ 3 rows in set (0.00 sec)
注意上面我们使用了%水%这一格式,这样使得水的位置可以随意变化,只要其中包含水字即可。
当然也可以只查询水字在末尾的情况:
1 2 3 4 5 6 7 8
mysql> select * from product p where p.name like '%水'; +----+-------+-----------+ | id | price | name | +----+-------+-----------+ | 1 | 10 | 矿泉水 | | 5 | 50 | 王水 | +----+-------+-----------+ 2 row in set (0.00 sec)
还可以查询商品名称只有2个字,且最后一个字包含水的商品:
1 2 3 4 5 6 7
mysql> select * from product p where p.name like '_水'; +----+-------+--------+ | id | price | name | +----+-------+--------+ | 5 | 50 | 王水 | +----+-------+--------+ 1 row in set (0.00 sec)
mysql> select * from product as p where p.price in (10,30,50); +----+-------+--------------+ | id | price | name | +----+-------+--------------+ | 1 | 10 | 矿泉水 | | 3 | 30 | 补水面膜 | | 5 | 50 | 王水 | +----+-------+--------------+ 3 rows in set (0.00 sec)
可以看到使用in查询比使用or查询语句要简介的多。
NOT IN查询
NOT IN查询和IN查询就刚好相反,IN是返回列表中满足指定条件的记录,而NOT IN则是返回列表中都不满足指定条件的记录。
NOT IN查询使用的语法格式如下所示:
1
select 列名 from 表名 where 字段 not in (值1,值2,值3,值4,...);
例子8:查询商品价格不是10,30,50的商品信息,如下所示:
1 2 3 4 5 6 7 8
mysql> select * from product as p where p.price not in (10,30,50); +----+-------+-----------+ | id | price | name | +----+-------+-----------+ | 2 | 20 | 火腿肠 | | 4 | 40 | 健力宝 | +----+-------+-----------+ 2 rows in set (0.00 sec)
mysql> select * from book1; +---+------+------+ | a | b | c | +---+------+------+ | 1 | 2 | a | | 3 | NULL | b | | 4 | 5 | NULL | +---+------+------+ 3 rows in set (0.00 sec)
mysql> select * from book1 where b <=0; Empty set (0.01 sec)
mysql> select * from book1 where b =NULL; Empty set (0.10 sec)
mysql> select * from book1 as b1 where b1.b between 0 and 10; +---+------+------+ | a | b | c | +---+------+------+ | 1 | 2 | a | | 4 | 5 | NULL | +---+------+------+ 2 rows in set (0.00 sec)
mysql> select * from book1 where c like '%'; +---+------+------+ | a | b | c | +---+------+------+ | 1 | 2 | a | | 3 | NULL | b | +---+------+------+ 2 rows in set (0.00 sec)
mysql> select * from book1 where c in ('a','b',NULL); +---+------+------+ | a | b | c | +---+------+------+ | 1 | 2 | a | | 3 | NULL | b | +---+------+------+ 2 rows in set (0.00 sec)
mysql> select * from book1 where c not in ('a','b',NULL); Empty set (0.00 sec)
mysql> select * from book2; +------+------+ | a | b | +------+------+ | 1 | a | | NULL | b | | 3 | NULL | | NULL | NULL | | 4 | c | +------+------+ 5 rows in set (0.00 sec)
mysql> select * from book2 as b where b.a is null; +------+------+ | a | b | +------+------+ | NULL | b | | NULL | NULL | +------+------+ 2 rows in set (0.13 sec)
mysql> select * from book2 as b where b.a is null or b.b is null; +------+------+ | a | b | +------+------+ | NULL | b | | 3 | NULL | | NULL | NULL | +------+------+ 3 rows in set (0.10 sec)
IS NOT NULL查询
IS NOT NULL查询用于返回值不为空的记录,其对应的语法格式如下所示:
1
select 列名 from 表名 where 列 is not null;
该语法用于查询指定的列的值不为NULL的记录。
例子10:下面的例子展示了,如何使用IS NOT NULL来查询指定的列的值不为NULL的记录,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
mysql> select * from book2 as b where b.a is not null; +------+------+ | a | b | +------+------+ | 1 | a | | 3 | NULL | | 4 | c | +------+------+ 3 rows in set (0.00 sec)
mysql> select * from book2 as b where b.a is not null and b.b is not null; +------+------+ | a | b | +------+------+ | 1 | a | | 4 | c | +------+------+ 2 rows in set (0.00 sec)
mysql> select * from book2; +------+------+ | a | b | +------+------+ | 1 | a | | NULL | b | | 3 | NULL | | NULL | NULL | | 4 | c | +------+------+ 5 rows in set (0.00 sec)
mysql> select * from book2 where a <=> null; +------+------+ | a | b | +------+------+ | NULL | b | | NULL | NULL | +------+------+ 2 rows in set (0.00 sec)
mysql> select * from book2 where a <=> 1; +------+------+ | a | b | +------+------+ | 1 | a | +------+------+ 1 row in set (0.00 sec)
经典面试题
请问下面两个SQL的查询结果一致么?
1 2
select * from product; select * from product where name like '%';