MySQL(二)
is null , is not null
演示: is null is not null
create table t1 (id int,name varchar(10)); # 创建
insert into t1(id) values(1),(2);
insert into t1 values (3,'qiye');
select * from t1 where name is null;
select * from t1 where name is not null;
and,or,not
select number from students where 16<= age and age<=21;
特殊:select number from students where 16<=age<=21; #相当于or,但基本不这么用
select * from students where age<=16 or age>=22;
select * from students where not age=16; 把age=16的排除掉,然后输出
select * from students where age=16 and ... 可以依次往后写条件
优先级:not>and>or
排序
select * from students order by age; # 正序
select * from students order by age desc; # 倒序
限制
#原数据表
+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 201804001 | Qiye | 16 | 2002-01-01 |
| 201804002 | JackLee | 17 | 2001-01-02 |
| 201804003 | Julia | 18 | 2000-01-03 |
| 201804004 | Stefer | 19 | 2001-01-04 |
| 201804005 | Steven | 20 | 2000-01-05 |
| 201804006 | Mark | 21 | 1999-01-06 |
| 201804007 | Stark | 22 | 1999-01-07 |
| 201804008 | Tonny | 23 | 1999-01-08 |
| 201804009 | Jarvis | 24 | 1999-01-09 |
| 201804010 | ZhangSan | 25 | 1999-01-10 |
| 201804011 | lisi | 23 | 1990-04-10 |
| 201804012 | wanger | 20 | 1991-01-10 |
| 201804013 | mazi | 21 | 1993-01-13 |
| 201804014 | xiaoxing | 80 | 1995-01-22 |
| 201804015 | hundan | 36 | 1998-05-10 |
| 201804016 | xiaowang | 30 | 1999-07-10 |
| 201804017 | laowang | 18 | 1997-08-10 |
+-----------+----------+------+------------+
select * from students limit 5; #输出5条,按表的顺序从上到下输出5条
+-----------+---------+------+------------+
| number | name | age | birth |
+-----------+---------+------+------------+
| 201804001 | Qiye | 16 | 2002-01-01 |
| 201804002 | JackLee | 17 | 2001-01-02 |
| 201804003 | Julia | 18 | 2000-01-03 |
| 201804004 | Stefer | 19 | 2001-01-04 |
| 201804005 | Steven | 20 | 2000-01-05 |
+-----------+---------+------+------------+
select * from students limit 3,3; #从第4条开始输出3条 计算机内部的排序是从0开始,0.1.2.3.4.5...,不是1
去重
select * from grades;
select distinct 字段 from 表名; #不会改变原表格,会把去重完的结果输出出来
模糊查询
select * from students;
select * from students where name like 'xi%';# 任意多个 %:匹配多个,可以有任意多个,xi开头的都输出出来
select * from students where name like 'li__';# 匹配任意一个,后面几个横线就代表li什么什么,共三个字,输出li什么什么,此处为两个
范围查询
select * from students where age in (1,16,22); #查找1,16,22的数
select * from students where age between 16 and 22; #一个区间内的所有数据
聚合函数
| 函数名 | 作用 |
|---|---|
| max | 最大值 |
| min | 最小值 |
| count | 计数 |
| sum | 总和 |
| avg | 平均值 |
#原表数据
+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 201804001 | Qiye | 16 | 2002-01-01 |
| 201804002 | JackLee | 17 | 2001-01-02 |
| 201804003 | Julia | 18 | 2000-01-03 |
| 201804004 | Stefer | 19 | 2001-01-04 |
| 201804005 | Steven | 20 | 2000-01-05 |
| 201804006 | Mark | 21 | 1999-01-06 |
| 201804007 | Stark | 22 | 1999-01-07 |
| 201804008 | Tonny | 23 | 1999-01-08 |
| 201804009 | Jarvis | 24 | 1999-01-09 |
| 201804010 | ZhangSan | 25 | 1999-01-10 |
| 201804011 | lisi | 23 | 1990-04-10 |
| 201804012 | wanger | 20 | 1991-01-10 |
| 201804013 | mazi | 21 | 1993-01-13 |
| 201804014 | xiaoxing | 80 | 1995-01-22 |
| 201804015 | hundan | 36 | 1998-05-10 |
| 201804016 | xiaowang | 30 | 1999-07-10 |
| 201804017 | laowang | 18 | 1997-08-10 |
+-----------+----------+------+------------+
(1)select *from students;
select count(*) from students; 统计数量 #()里可以换别的字段,它只会统计数量,不看内容是什么
(2)select avg(age) from students; 平均年龄 #括号里字段里的内容必须是数字,否则会返回零,但不报错
(3)select group_concat(name) from students; 返回连接的字符串
#输出:
+---------------------------------------------------------------------------------------------------------------------+
| group_concat(name) |
+---------------------------------------------------------------------------------------------------------------------+
| Qiye,JackLee,Julia,Stefer,Steven,Mark,Stark,Tonny,Jarvis,ZhangSan,lisi,wanger,mazi,xiaoxing,hundan,xiaowang,laowang |
+---------------------------------------------------------------------------------------------------------------------+
select group_concat(age) from students;
#输出:
+----------------------------------------------------+
| group_concat(age) |
+----------------------------------------------------+
| 16,17,18,19,20,21,22,23,24,25,23,20,21,80,36,30,18 |
+----------------------------------------------------+
(4)select max(age) from students; 年龄最大
(5)select min(age) from students; 年龄最小
分组
#原数据表格
+----------------+----------------+-------+
| student_number | subject_number | grade |
+----------------+----------------+-------+
| 201804001 | 0001 | 90 |
| 201804002 | 0001 | 89 |
| 201804003 | 0001 | 88 |
| 201804004 | 0001 | 87 |
| 201804005 | 0001 | 86 |
| 201804006 | 0001 | 85 |
| 201804007 | 0001 | 84 |
| 201804008 | 0001 | 83 |
| 201804009 | 0001 | 82 |
| 201804017 | 0001 | 91 |
| 201804016 | 0001 | 91 |
| 201804011 | 0001 | 91 |
| 201804010 | 0001 | 81 |
| 201804001 | 0002 | 80 |
| 201804002 | 0002 | 79 |
| 201804003 | 0002 | 78 |
| 201804004 | 0002 | 77 |
| 201804005 | 0002 | 76 |
| 201804006 | 0002 | 75 |
| 201804007 | 0002 | 74 |
| 201804008 | 0002 | 73 |
| 201804009 | 0002 | 72 |
| 201804010 | 0002 | 71 |
| 201804012 | 0001 | 91 |
| 201804013 | 0001 | 91 |
| 201804014 | 0001 | 91 |
| 201804015 | 0001 | 91 |
| 201804016 | 0001 | 91 |
+----------------+----------------+-------+
(1)select subject_number from grades group by subject_number; # 去重+排序,去重完每类都为一组,这里数据只有0001和0002,去重完是两组
+----------------+
| subject_number |
+----------------+
| 0001 |
| 0002 |
+----------------+
select subject_number,count(*) from grades group by subject_number; #展示每组有几个,展示的组必须是上一步分的组
+----------------+----------+
| subject_number | count(*) |
+----------------+----------+
| 0001 | 18 |
| 0002 | 10 |
+----------------+----------+
#必须先分组,再展示
select subject_number,count(grade) from grades group by subject_number;
+----------------+--------------+
| subject_number | count(grade) |
+----------------+--------------+
| 0001 | 18 |
| 0002 | 10 |
+----------------+--------------+
select subject_number,count(student_number) from grades group by subject_number;
+----------------+-----------------------+
| subject_number | count(student_number) |
+----------------+-----------------------+
| 0001 | 18 |
| 0002 | 10 |
+----------------+-----------------------+
注:分好组后,还可以对他进行条件限制:
(2)select subject_number,count(*) from grades group by subject_number,grade having grade>=80; #分两次组
#输出:
+----------------+----------+
| subject_number | count(*) |
+----------------+----------+
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 1 |
| 0001 | 8 |
| 0002 | 1 |
+----------------+----------+
select
subject_number,count(*)