可以先参考上篇内容:SQL语言之基础查询(一)
文章目录
7、条件查询:where
上述说的查询方法都是针对一个表的字段进行,但是如果有时候需要查询行数据时,该怎么查询,比如查询一个成绩表,我只想看某个人的成绩,也就是一个成绩表的某一行,这个时候该怎么筛选呢?这就要条件查询了!
条件查询就是通过指定具体的条件,按照特定条件对数据进行筛选。
语法:
select 列名|* from 表名 where 条件判断;
具体实现:遍历表中每一行数据,如果数据满足条件,则放到结果集中,否则就pass
其中条件判断组成:列名 运算符 数值
运算符有下面几种:
比较运算符:
>,>=,<,<= |
大于,大于等于,小于,小于等于 |
---|---|
<=> |
等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) |
= |
等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL |
!=,<> |
不等于 |
between a0 and a1 |
范围匹配,[a0, a1] ,如果 a0 <= value <= a1 ,返回 TRUE(1) |
in(option,...) |
如果是 option 中的任意一个,返回 TRUE(1) |
is null |
是 NULL |
is not null |
不是 NULL |
like |
模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符 |
逻辑运算符:
运算符 | 说明 |
---|---|
AND |
多个条件必须都为 TRUE(1) ,结果才是 TRUE(1) |
OR |
任意一个条件为 TRUE(1) , 结果为 TRUE(1) |
NOT |
条件为 TRUE(1) ,结果为 FALSE(0) |
案例:
7.1、基础查询
1、查询英语不及格的同学及英语成绩 ( < 60 )
mysql> select name,english from exam_result;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56.0 |
| 孙悟空 | 77.0 |
| 猪悟能 | 90.0 |
| 曹孟德 | 67.0 |
| 刘玄德 | 45.0 |
| 孙权 | 78.5 |
| 宋公明 | 30.0 |
| 熊大 | 30.0 |
| 熊大 | 30.0 |
+-----------+---------+
9 rows in set (0.00 sec)
mysql> select name,english from exam_result where english < 60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56.0 |
| 刘玄德 | 45.0 |
| 宋公明 | 30.0 |
| 熊大 | 30.0 |
| 熊大 | 30.0 |
+-----------+---------+
5 rows in set (0.00 sec)
结合之前学习内容综合练习:
想将英语不及格的人进行排序:
mysql> select name,english from exam_result order by english where english < 60;
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'where english < 60' at line 1
发现报错!!,为什么呢??还是之前分析的SQL执行顺序,现在增加了条件选择,那我们可以思考一下是先判断条件呢?还是排序呢?是不是我们要加上判断条件,然后根据这个条件筛选出一个临时表,然后根据这个临时表在进行排序呢?
对的!!因为如果先排序,排完之后还是要根据条件筛选数据,那这样其实就没必要把所有数据进行排序,毕竟排序是需要时间的,所以是先执行条件判断,才进行排序的。
mysql> select name,english from exam_result where english < 60 order by english;
+-----------+---------+
| name | english |
+-----------+---------+
| 宋公明 | 30.0 |
| 熊大 | 30.0 |
| 熊大 | 30.0 |
| 刘玄德 | 45.0 |
| 唐三藏 | 56.0 |
+-----------+---------+
5 rows in set (0.00 sec)
-- 和去重一起结合实现
mysql> select distinct name,english from exam_result where english < 60 order by english;
+-----------+---------+
| name | english |
+-----------+---------+
| 宋公明 | 30.0 |
| 熊大 | 30.0 |
| 刘玄德 | 45.0 |
| 唐三藏 | 56.0 |
+-----------+---------+
4 rows in set (0.01 sec)
【分析】:
select distinct name,english from exam_result where english < 60 order by english;
执行顺序分析
- 先查询
from
后的表exam_result
,查询的字段是select
后的字段。 - 然后因为
distinct
修饰所有字段,所以将重复的行删除。 接下来执行~~where~~
条件,根据条件,从2中生成的临时表的每一行筛选条件,满足的组成一个新临时表。- 其实是
where
条件和字段查询是一起的,也就是根据where
条件,同时结合指定字段进行查询。(现在查询的字段不能显示这个说法,下面会验证这个说法) order by
最后执行,根据限定的字段,然后进行最后排序。
2、查询语文成绩比英语成绩好的学生
-- 查询语文和英语成绩,并按照语文成绩升序,语文成绩相同的英语成绩降序排序
mysql> select name,chinese,english from exam_result order by chinese,english desc;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 刘玄德 | 55.5 | 45.0 |
| 唐三藏 | 67.0 | 56.0 |
| 孙权 | 70.0 | 78.5 |
| 宋公明 | 75.0 | 30.0 |
| 熊大 | 75.0 | 30.0 |
| 熊大 | 75.0 | 30.0 |
| 曹孟德 | 82.0 | 67.0 |
| 孙悟空 | 87.5 | 77.0 |
| 猪悟能 | 88.0 | 90.0 |
+-----------+---------+---------+
9 rows in set (0.00 sec)
mysql> select name,chinese,english from exam_result where chinese > english;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 唐三藏 | 67.0 | 56.0 |
| 孙悟空 | 87.5 | 77.0 |
| 曹孟德 | 82.0 | 67.0 |
| 刘玄德 | 55.5 | 45.0 |
| 宋公明 | 75.0 | 30.0 |
| 熊大 | 75.0 | 30.0 |
| 熊大 | 75.0 | 30.0 |
+-----------+---------+---------+
7 rows in set (0.00 sec)
3、查询总分在200分以下同学
要先查询总分,然后根据总分进行筛选
-- 查询总分
mysql> select name,chinese + math + english from exam_result;
+-----------+--------------------------+
| name | chinese + math + english |
+-----------+--------------------------+
| 唐三藏 | 221.0 |
| 孙悟空 | 242.5 |
| 猪悟能 | 276.5 |
| 曹孟德 | 233.0 |
| 刘玄德 | 185.5 |
| 孙权 | 221.5 |
| 宋公明 | 170.0 |
| 熊大 | 170.0 |
| 熊大 | 183.0 |
+-----------+--------------------------+
9 rows in set (0.00 sec)
mysql> select name,chinese + math + english from exam_result where chinese + math + english < 200;
+-----------+--------------------------+
| name | chinese + math + english |
+-----------+--------------------------+
| 刘玄德 | 185.5 |
| 宋公明 | 170.0 |
| 熊大 | 170.0 |
| 熊大 | 183.0 |
+-----------+--------------------------+
4 rows in set (0.00 sec)
用where
和as
结合查询
mysql> select name,chinese + math + english as 总分 from exam_result;
+-----------+--------+
| name | 总分 |
+-----------+--------+
| 唐三藏 | 221.0 |
| 孙悟空 | 242.5 |
| 猪悟能 | 276.5 |
| 曹孟德 | 233.0 |
| 刘玄德 | 185.5 |
| 孙权 | 221.5 |
| 宋公明 | 170.0 |
| 熊大 | 183.0 |
| 熊大 | 183.0 |
+-----------+--------+
9 rows in set (0.00 sec)
mysql> select name,chinese + math + english as 总分 from exam_result where 总分 < 200;
ERROR 1054 (42S22): Unknown column '总分' in 'where clause'
得出结论:where
条件就是和 select
后的字段一起进行查询的,而 as
则是在 where
执行之后才进行命名的:
-- 说明where和字段查询是一起的,然后才是as
mysql> select name,chinese + math + english as 总分 from exam_result where chinese + math + english < 200;
+-----------+--------+
| name | 总分 |
+-----------+--------+
| 刘玄德 | 185.5