1、判断是否为空
is null:当列的值为NULL时,此条件为真。
is not null:当列的值不为NULL时,此条件为真。
create table t1(id int, name varchar(10));
insert into t1(id) values(1),(2);
insert into t1 values(3,'sunwukong');
select * from t1 where name is null;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
+------+------+
2 rows in set (0.01 sec)
select * from t1 where name is not null;
+------+-----------+
| id | name |
+------+-----------+
| 3 | sunwukong |
+------+-----------+
1 row in set (0.00 sec)
2、逻辑判断
and:与,需要同时满足where子句中的条件。
or:或,只需要匹配多个where子句中的一个条件。
not:非,一般与in、between and、exists一起使用,表示取反。
select * from students where 16<=age and age<=21;
+-----------+---------+------+------------+
| number | name | age | birth |
+-----------+---------+------+------------+
| 202404001 | Qiye | 16 | 2002-01-01 |
| 202404002 | JackLee | 17 | 2001-01-02 |
| 202404003 | Julia | 18 | 2000-01-03 |
| 202404004 | Stefer | 19 | 2001-01-04 |
| 202404005 | Steven | 20 | 2000-01-05 |
| 202404006 | Mark | 21 | 1999-01-06 |
| 202404012 | wanger | 20 | 1991-01-10 |
| 202404013 | mazi | 21 | 1993-01-13 |
| 202404017 | laowang | 18 | 1997-08-10 |
+-----------+---------+------+------------+
9 rows in set (0.01 sec)
select * from students where age<=16 or age>=22;
+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 202404001 | Qiye | 16 | 2002-01-01 |
| 202404007 | Stark | 22 | 1999-01-07 |
| 202404008 | Tonny | 23 | 1999-01-08 |
| 202404009 | Jarvis | 24 | 1999-01-09 |
| 202404010 | ZhangSan | 25 | 1999-01-10 |
| 202404011 | lisi | 23 | 1990-04-10 |
| 202404014 | xiaoxing | 80 | 1995-01-22 |
| 202404015 | hundan | 36 | 1998-05-10 |
| 202404016 | xiaowang | 30 | 1999-07-10 |
+-----------+----------+------+------------+
9 rows in set (0.01 sec)
select * from students where not age=16;
+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 202404002 | JackLee | 17 | 2001-01-02 |
| 202404003 | Julia | 18 | 2000-01-03 |
| 202404004 | Stefer | 19 | 2001-01-04 |
| 202404005 | Steven | 20 | 2000-01-05 |
| 202404006 | Mark | 21 | 1999-01-06 |
| 202404007 | Stark | 22 | 1999-01-07 |
| 202404008 | Tonny | 23 | 1999-01-08 |
| 202404009 | Jarvis | 24 | 1999-01-09 |
| 202404010 | ZhangSan | 25 | 1999-01-10 |
| 202404011 | lisi | 23 | 1990-04-10 |
| 202404012 | wanger | 20 | 1991-01-10 |
| 202404013 | mazi | 21 | 1993-01-13 |
| 202404014 | xiaoxing | 80 | 1995-01-22 |
| 202404015 | hundan | 36 | 1998-05-10 |
| 202404016 | xiaowang | 30 | 1999-07-10 |
| 202404017 | laowang | 18 | 1997-08-10 |
+-----------+----------+------+------------+
16 rows in set (0.01 sec)
3、排序
order by:用于对结果集进行排序。
# 正序
select * from students order by age;
+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 202404001 | Qiye | 16 | 2002-01-01 |
| 202404002 | JackLee | 17 | 2001-01-02 |
| 202404003 | Julia | 18 | 2000-01-03 |
| 202404017 | laowang | 18 | 1997-08-10 |
| 202404004 | Stefer | 19 | 2001-01-04 |
| 202404005 | Steven | 20 | 2000-01-05 |
| 202404012 | wanger | 20 | 1991-01-10 |
| 202404006 | Mark | 21 | 1999-01-06 |
| 202404013 | mazi | 21 | 1993-01-13 |
| 202404007 | Stark | 22 | 1999-01-07 |
| 202404008 | Tonny | 23 | 1999-01-08 |
| 202404011 | lisi | 23 | 1990-04-10 |
| 202404009 | Jarvis | 24 | 1999-01-09 |
| 202404010 | ZhangSan | 25 | 1999-01-10 |
| 202404016 | xiaowang | 30 | 1999-07-10 |
| 202404015 | hundan | 36 | 1998-05-10 |
| 202404014 | xiaoxing | 80 | 1995-01-22 |
+-----------+----------+------+------------+
17 rows in set (0.02 sec)
# 倒序
select * from students order by age desc;
+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 202404014 | xiaoxing | 80 | 1995-01-22 |
| 202404015 | hundan | 36 | 1998-05-10 |
| 202404016 | xiaowang | 30 | 1999-07-10 |
| 202404010 | ZhangSan | 25 | 1999-01-10 |
| 202404009 | Jarvis | 24 | 1999-01-09 |
| 202404008 | Tonny | 23 | 1999-01-08 |
| 202404011 | lisi | 23 | 1990-04-10 |
| 202404007 | Stark | 22 | 1999-01-07 |
| 202404006 | Mark | 21 | 1999-01-06 |
| 202404013 | mazi | 21 | 1993-01-13 |
| 202404005 | Steven | 20 | 2000-01-05 |
| 202404012 | wanger | 20 | 1991-01-10 |
| 202404004 | Stefer | 19 | 2001-01-04 |
| 202404003 | Julia | 18 | 2000-01-03 |
| 202404017 | laowang | 18 | 1997-08-10 |
| 202404002 | JackLee | 17 | 2001-01-02 |
| 202404001 | Qiye | 16 | 2002-01-01 |
+-----------+----------+------+------------+
17 rows in set (0.00 sec)
4、限制
limit:限制select查询返回的行数
# 展示5条数据
select * from students limit 5;
+-----------+---------+------+------------+
| number | name | age | birth |
+-----------+---------+------+------------+
| 202404001 | Qiye | 16 | 2002-01-01 |
| 202404002 | JackLee | 17 | 2001-01-02 |
| 202404003 | Julia | 18 | 2000-01-03 |
| 202404004 | Stefer | 19 | 2001-01-04 |
| 202404005 | Steven | 20 | 2000-01-05 |
+-----------+---------+------+------------+
5 rows in set (0.01 sec)
# 从第4行数据开始输出3条
select * from students limit 3,3;
+-----------+--------+------+------------+
| number | name | age | birth |
+-----------+--------+------+------------+
| 202404004 | Stefer | 19 | 2001-01-04 |
| 202404005 | Steven | 20 | 2000-01-05 |
| 202404006 | Mark | 21 | 1999-01-06 |
+-----------+--------+------+------------+
3 rows in set (0.00 sec)
5、去重
distinct:过滤重复数据
select distinct subject_number from grades;
+----------------+
| subject_number |
+----------------+
| 0001 |
| 0002 |
+----------------+
2 rows in set (0.00 sec)
6、模糊查询
like:根据制定的模式过滤数据
# 查询name以xi开头的数据,%任意多个
select * from students where name like 'xi%';
+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 202404014 | xiaoxing | 80 | 1995-01-22 |
| 202404016 | xiaowang | 30 | 1999-07-10 |
+-----------+----------+------+------------+
2 rows in set (0.02 sec)
# _匹配单个字符,用于匹配任意单个字符
select * from students where name like 'li_';
select * from students where name like 'li__';
+-----------+------+------+------------+
| number | name | age | birth |
+-----------+------+------+------------+
| 202404011 | lisi | 23 | 1990-04-10 |
+-----------+------+------+------------+
1 row in set (0.00 sec)
7、范围查询
betweent:用于选择介于两个值之间的数据,常与and连起来使用
in:返回在指定范围内的数据
# age 在(1,16,17,22)这里面的就全部展示
select * from students where age in(1,16,17,22);
+-----------+---------+------+------------+
| number | name | age | birth |
+-----------+---------+------+------------+
| 202404001 | Qiye | 16 | 2002-01-01 |
| 202404002 | JackLee | 17 | 2001-01-02 |
| 202404007 | Stark | 22 | 1999-01-07 |
+-----------+---------+------+------------+
3 rows in set (0.01 sec)
# 一个区间内所有的数据,16到22之间所有的数据
select * from students where age between 16 and 22;
+-----------+---------+------+------------+
| number | name | age | birth |
+-----------+---------+------+------------+
| 202404001 | Qiye | 16 | 2002-01-01 |
| 202404002 | JackLee | 17 | 2001-01-02 |
| 202404003 | Julia | 18 | 2000-01-03 |
| 202404004 | Stefer | 19 | 2001-01-04 |
| 202404005 | Steven | 20 | 2000-01-05 |
| 202404006 | Mark | 21 | 1999-01-06 |
| 202404007 | Stark | 22 | 1999-01-07 |
| 202404012 | wanger | 20 | 1991-01-10 |
| 202404013 | mazi | 21 | 1993-01-13 |
| 202404017 | laowang | 18 | 1997-08-10 |
+-----------+---------+------+------------+
10 rows in set (0.00 sec)
8、聚合函数
count(*):统计所有的记录,包括null
count(字段):统计该字段的所有信息
sum(字段):统计表中该字段的和
avg(字段):统计表中该字段的平均值
# 统计该表中总共有多少行数据
select count(*) from students;
+----------+
| count(*) |
+----------+
| 17 |
+----------+
1 row in set (0.01 sec)
# 统计该表中有多少个name
select count(name) from students;
+-------------+
| count(name) |
+-------------+
| 17 |
+-------------+
1 row in set (0.00 sec)
# 统计该表中所有age的和
select sum(age) from students;
+----------+
| sum(age) |
+----------+
| 433 |
+----------+
1 row in set (0.01 sec)
# 统计该表中所有age的平均数
select avg(age) from students;
+----------+
| avg(age) |
+----------+
| 25.4706 |
+----------+
1 row in set (0.01 sec)
9、分组
group by 列名 having 条件表达式
group by:可用于单个字段分组也可用于多个字段分组。
having 条件表达式:用于过滤分组后的数据
# 通过subject_number,grade进行分组,然后以成绩大于80(包含80)做和筛选
select subject_number,grade,count(*) from grades group by subject_number,grade having grade>=80;
+----------------+-------+----------+
| subject_number | grade | count(*) |
+----------------+-------+----------+
| 0001 | 81 | 1 |
| 0001 | 82 | 1 |
| 0001 | 83 | 1 |
| 0001 | 84 | 1 |
| 0001 | 85 | 1 |
| 0001 | 86 | 1 |
| 0001 | 87 | 1 |
| 0001 | 88 | 1 |
| 0001 | 89 | 1 |
| 0001 | 90 | 1 |
| 0001 | 91 | 8 |
| 0002 | 80 | 1 |
+----------------+-------+----------+
12 rows in set (0.01 sec)
10、子查询
select * from (select * from students order by age limit 5) as sunwukong where age <18;
+-----------+---------+------+------------+
| number | name | age | birth |
+-----------+---------+------+------------+
| 202404001 | Qiye | 16 | 2002-01-01 |
| 202404002 | JackLee | 17 | 2001-01-02 |
+-----------+---------+------+------------+
2 rows in set (0.01 sec)
11、作业
# 统计出0001和0002分别有多少人
select count(*) from grades where subject_number='0001';
select count(*) from grades where subject_number='0002';
# 统计出0001中age 大于18岁的人数
select count(*) from grades m,students n where m.subject_number='0001' and m.student_number=n.number and n.age>18;