条件查询
-
select 列名 from 表名 where 列 运算符 值
- 注意关键字where,where后面跟上一个或者多个条件,条件是对前面数据的过
滤,只有满足where后面条件的数据才会被返回
- 等于(=)
select * from test1 where b = 'abc';
select * from test1 where a<>1;
select * from test1 where a!=1;
-- sql语句中尽量使用<>来做不等判断
-- 数值按照大小比较
select * from test1 where a>1;
-- 字符按照ASCII码对应的值进行比较,比较时按照字符对应的位置一个字符一个
字符的⽐较
select * from test1 where b>'a';
select * from test1 where b>'ac';
逻辑运算符
- AND (并且)
- select 列名 from 表名 where 条件1 and 条件2;
- 表示返回满足条件1和条件2的记录
/*
mysql> select * from test3;
+---+---+
| a | b |
+---+---+
| 1 | a |
| 2 | b |
| 2 | c |
| 3 | c |
+---+---+
4 rows in set (0.00 sec)
*/
select * from test3 t where t.a=2 and t.b='c';
/*
+---+---+
| a | b |
+---+---+
| 2 | c |
+---+---+
1 row in set (0.00 sec)
*/
- OR (或者)
- select 列名 from 表名 where 条件1 or 条件2;
- 满足条件1或者满足条件2的记录都会被返回
select * from test3 t where t.a=1 or t.b='c';
- like (模糊查询)
- select 列名 from 表名 where 列 like pattern;
- pattern中可以包含通配符,有以下通配符:
- %:表示匹配任意一个或多个字符
- _:表示匹配任意一个字符
select * from stu a where a.name like '张%';
select * from stu a where a.name like '%学%';
select * from stu a where a.name like '张_';
- BETWEEN AND (区间查询)
-
select 列名 from 表名 where 列名 between 值1 and 值2;
- 返回对应的列的值在[值1,值2]区间中的记录
- 使用between and可以提高语句的简洁度
- 两个临界值不要调换位置,只能是大于等于左边的值,并且小于等于右边的值
select * from stu t where t.age between 25 and 32;
/*
+----+-----+-----------+
| id | age | name |
+----+-----+-----------+
| 2 | 25 | 李四 |
| 3 | 26 | 张学友 |
| 4 | 32 | 刘德华 |
+----+-----+-----------+
3 rows in set (0.00 sec)
*/
select * from stu t where t.age >= 25 and t.age <= 32;
- IN 查询
-
select 列名 from 表名 where 字段 in (值1,值2,值3,值4);
- in 后面括号中可以包含多个值,对应记录的字段满足in中任意一个都会被返回
- in列表的值类型必须一致或兼容
- in列表中不支持通配符
select * from test6 t where t.age in (10,15,20,30);
/*
+------+------+
| id | age |
+------+------+
| 2 | 15 |
| 4 | 20 |
| 6 | 10 |
| 7 | 10 |
| 8 | 30 |
+------+------+
5 rows in set (0.00 sec)
*/
- NOT IN 查询
- select 列名 from 表名 where 字段 not in (值1,值2,值3,值4);
select * from test6 t where t.age not in (10,15,20,30);
/*
+------+------+
| id | age |
+------+------+
| 1 | 14 |
| 3 | 18 |
| 5 | 28 |
+------+------+
3 rows in set (0.00 sec)
*/
- IS NULL / IS NOT NULL
-
select 列名 from 表名 where 列 is null;
-
select 列名 from 表名 where 列 is not null;
select * from test7 t where t.a is null;
/*
+------+------+
| a | b |
+------+------+
| NULL | b |
| NULL | NULL |
+------+------+
2 rows in set (0.00 sec)
*/
select * from test7 t where t.a is null or t.b is null;
/*
+------+------+
| a | b |
+------+------+
| NULL | b |
| 3 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)
*/
select * from test7 t where t.a is not null;
/*
+------+------+
| a | b |
+------+------+
| 1 | a |
| 3 | NULL |
| 4 | c |
+------+------+
3 rows in set (0.00 sec)
*/
select * from test7 t where t.a is not null and t.b is not
null;
/*
+------+------+
| a | b |
+------+------+
| 1 | a |
| 4 | c |
+------+------+
2 rows in set (0.00 sec)
*/
- <=> 安全等于
- 既可以判断NULL值,又可以判断普通的数值,可读性较低,用得较少
/*
mysql> select * from test8;
+------+------+
| a | b |
+------+------+
| 1 | a |
| NULL | b |
| 3 | NULL |
| NULL | NULL |
| 4 | c |
+------+------+
5 rows in set (0.00 sec)
*/
select * from test8 t where t.a<=>null;
/*
+------+------+
| a | b |
+------+------+
| NULL | b |
| NULL | NULL |
+------+------+
2 rows in set (0.00 sec)
*/
select * from test8 t where t.a<=>null;
/*
+------+------+
| a | b |
+------+------+
| NULL | b |
| NULL | NULL |
+------+------+
2 rows in set (0.00 sec)
*/
select * from test8 t where t.a<=>1;
/*
+------+------+
| a | b |
+------+------+
| 1 | a |
+------+------+
1 row in set (0.00 sec)
*/
经典面试题
-- 下面的2个sql查询结果一样么?
select * from students;
select * from students where name like '%';
--
-- 当name没有NULL值时,返回的结果一样。
-- 当name有NULL值时,第2个sql查询不出name为NULL的记录
总结
- like中的%可以匹配一个到多个任意的字符,_可以匹配任意一个字符
- 空值查询需要使用IS NULL或者IS NOT NULL,其他查询运算符对NULL值无效
- 建议创建表的时候,尽量设置表的字段不能为空,给字段设置一个默认值
- <=>(安全等于)玩玩可以,建议少使用
网友评论