DML语句:
SELECT select-list FROM tb WHERE qualification;
FROM子句:要查询的关系 表、多个表、其他的select语句
WHERE子句:布尔型关系表达式 ,还可以是算术运算表达式
符号 | 代表含义 |
---|---|
= | 等于 |
<> 或者 != | 不等于 |
<=> | 有NULL也能进行比较,等于 |
< | 小于 |
<= | 小于等于 |
= |
大于等于 |
< | 小于 |
> | 大于 |
+、-、*、/、% | 加、减、乘、除、取余 |
and 或者 && | 逻辑与 |
or 或者 | |
not 或者 ! | 逻辑非 |
xor | 异或 |
查询语句的类型:简单查询,多表查询,子查询
投影:显示所有行的指定的字段(部分字段)
SELECT field1,field2 FROM tb_name;
mysql> SELECT name,cid FROM student;
+------+-----+
| name | cid |
+------+-----+
| jim | 1 |
| tom | 2 |
| jack | 3 |
+------+-----+
3 rows in set (0.00 sec)
选择:显示表中与搜索码匹配的行(部分行)
SELECT * FROM tb_name WHERE qualification;
mysql> SELECT * FROM student WHERE id=3;
+----+------+-----+
| id | name | cid |
+----+------+-----+
| 3 | jack | 3 |
+----+------+-----+
1 row in set (0.00 sec)
查询的字段去重
mysql> select distinct gender from student;
+--------+
| gender |
+--------+
| M |
| F |
+--------+
2 rows in set (0.00 sec)
筛选大于20岁的所有学生
mysql> select * from student where age>=20;
+----+-------+-----+--------+-----+
| id | name | cid | gender | age |
+----+-------+-----+--------+-----+
| 1 | jim | 1 | M | 21 |
| 3 | jack | 3 | M | 20 |
| 4 | lucy | 2 | F | 25 |
| 5 | neccy | 3 | F | 30 |
+----+-------+-----+--------+-----+
4 rows in set (0.00 sec)
筛选大于20的男性同学
mysql> select * from student where age>=20 and gender='M';
+----+------+-----+--------+-----+
| id | name | cid | gender | age |
+----+------+-----+--------+-----+
| 1 | jim | 1 | M | 21 |
| 3 | jack | 3 | M | 20 |
+----+------+-----+--------+-----+
2 rows in set (0.00 sec)
筛选年龄不大于等于20 并且为男性的同学
mysql> select * from student where not age>=20 and gender='M';
+----+------+-----+--------+-----+
| id | name | cid | gender | age |
+----+------+-----+--------+-----+
| 2 | tom | 2 | M | 19 |
+----+------+-----+--------+-----+
1 row in set (0.00 sec)
年龄在20-30 之间的同学(大于20小于等于30)
mysql> select * from student where age between 20 and 30;
+----+-------+-----+--------+-----+
| id | name | cid | gender | age |
+----+-------+-----+--------+-----+
| 1 | jim | 1 | M | 21 |
| 3 | jack | 3 | M | 20 |
| 4 | lucy | 2 | F | 25 |
| 5 | neccy | 3 | F | 30 |
+----+-------+-----+--------+-----+
4 rows in set (0.00 sec)
通配符 | 含义 |
---|---|
% | 任意长度任意字符 |
_ | 任意单个字符 |
查询名字以j开头的同学
mysql> select * from student where name like 'j%';
+----+------+-----+--------+-----+
| id | name | cid | gender | age |
+----+------+-----+--------+-----+
| 1 | jim | 1 | M | 21 |
| 3 | jack | 3 | M | 20 |
+----+------+-----+--------+-----+
2 rows in set (0.00 sec)
姓名中以 j 开头的三个单词组成的同学
mysql> select * from student where name like 'j__';
+----+------+-----+--------+-----+
| id | name | cid | gender | age |
+----+------+-----+--------+-----+
| 1 | jim | 1 | M | 21 |
+----+------+-----+--------+-----+
1 row in set (0.00 sec)
查询姓名以 l、n、t 开头的所有同学
mysql> select * from student where name rlike '^[lnt].*';
+----+-------+-----+--------+-----+
| id | name | cid | gender | age |
+----+-------+-----+--------+-----+
| 2 | tom | 2 | M | 19 |
| 4 | lucy | 2 | F | 25 |
| 5 | neccy | 3 | F | 30 |
+----+-------+-----+--------+-----+
3 rows in set (0.00 sec)
筛选年龄为19、20、30的学生
mysql> select * from student where age in (19,20,30);
+----+-------+-----+--------+-----+
| id | name | cid | gender | age |
+----+-------+-----+--------+-----+
| 2 | tom | 2 | M | 19 |
| 3 | jack | 3 | M | 20 |
| 5 | neccy | 3 | F | 30 |
+----+-------+-----+--------+-----+
3 rows in set (0.00 sec)
筛选课程二字段为空的学生
mysql> select * from student where cid2 is null;
+----+-------+-----+--------+-----+------+
| id | name | cid | gender | age | cid2 |
+----+-------+-----+--------+-----+------+
| 2 | tom | 2 | M | 19 | NULL |
| 5 | neccy | 3 | F | 30 | NULL |
+----+-------+-----+--------+-----+------+
2 rows in set (0.00 sec)
mysql> select * from student where cid2 is not null;
+----+------+-----+--------+-----+------+
| id | name | cid | gender | age | cid2 |
+----+------+-----+--------+-----+------+
| 1 | jim | 1 | M | 21 | 1 |
| 3 | jack | 3 | M | 20 | 4 |
| 4 | lucy | 2 | F | 25 | 4 |
+----+------+-----+--------+-----+------+
3 rows in set (0.00 sec)
将查询结果通过姓名排序,正序和反序
mysql> select * from student where cid2 is not null order by name;
+----+------+-----+--------+-----+------+
| id | name | cid | gender | age | cid2 |
+----+------+-----+--------+-----+------+
| 3 | jack | 3 | M | 20 | 4 |
| 1 | jim | 1 | M | 21 | 1 |
| 4 | lucy | 2 | F | 25 | 4 |
+----+------+-----+--------+-----+------+
3 rows in set (0.00 sec)
mysql> select * from student where cid2 is not null order by name DESC;
+----+------+-----+--------+-----+------+
| id | name | cid | gender | age | cid2 |
+----+------+-----+--------+-----+------+
| 4 | lucy | 2 | F | 25 | 4 |
| 1 | jim | 1 | M | 21 | 1 |
| 3 | jack | 3 | M | 20 | 4 |
+----+------+-----+--------+-----+------+
3 rows in set (0.00 sec)
mysql> select name as stu_name from student where age >=20;
+----------+
| stu_name |
+----------+
| jim |
| jack |
| lucy |
| neccy |
+----------+
4 rows in set (0.00 sec)
LIMIT [offiset,]count
offset:偏移量
count:显示的总个数。
mysql> select name from student where age >=20;
+-------+
| name |
+-------+
| jim |
| jack |
| lucy |
| neccy |
+-------+
4 rows in set (0.00 sec)
mysql> select name from student where age >=20 limit 2;
+------+
| name |
+------+
| jim |
| jack |
+------+
2 rows in set (0.00 sec)
mysql> select name from student where age >=20 limit 2,2;
+-------+
| name |
+-------+
| lucy |
| neccy |
+-------+
2 rows in set (0.00 sec)
MAX()、MIN()、SUM()、AVG()、COUNT()
显示男同学和女同学的平均年龄
mysql> select avg(age) from student where gender='M';
+----------+
| avg(age) |
+----------+
| 20.0000 |
+----------+
1 row in set (0.00 sec)
mysql> select avg(age) from student where gender='F';
+----------+
| avg(age) |
+----------+
| 27.5000 |
+----------+
1 row in set (0.00 sec)
根据相同规则的分组,利用聚合函数
求男、女同学的平均年龄
mysql> select avg(age),gender from student group by gender;
+----------+--------+
| avg(age) | gender |
+----------+--------+
| 20.0000 | M |
| 27.5000 | F |
+----------+--------+
2 rows in set (0.00 sec)
HAVING 和GROUP BY 连用,用于过滤满足条件的内容。
打印同一门课,选修人数超过2个的选修人数以及课程ID。
mysql> select count(name) as num_person,cid from student group by cid having num_person >= 2;
+------------+-----+
| num_person | cid |
+------------+-----+
| 2 | 2 |
| 2 | 3 |
+------------+-----+
2 rows in set (0.00 sec)
连接方式 | 举例 |
---|---|
交叉连接 | 笛卡尔乘积(a+b)(c+d)= ac+ad + bc+bd,表A和表B的每一行都做匹配 |
自然连接(内连接) | 两张表中对应字段存在等值关系 |
外连接 | 两张表中,不是完全的等值关系,表A和表B,以某字段作为连接关系,包括左外连接和右外连接。 |
左外连接 | 两张表中,不是完全的等值关系,显示所有学生的选修情况,如果还没有选择,选修课为空。 … LEFT JOIN … ON … |
右外连接 | 两张表中,不是完全的等值关系,显示所有学生的选修情况,如果有一门课没有人选择,学生姓名显示为空 … RIGHT JOIN … ON … |
自连接 | 一样表,不同的字段确立等值关系。 |
mysql> select * from student;
+----+-------+-----+--------+-----+------+
| id | name | cid | gender | age | cid2 |
+----+-------+-----+--------+-----+------+
| 1 | jim | 1 | M | 21 | 1 |
| 2 | tom | 2 | M | 19 | NULL |
| 3 | jack | 3 | M | 20 | 4 |
| 4 | lucy | 2 | F | 25 | 4 |
| 5 | neccy | 3 | F | 30 | NULL |
| 6 | mary | 4 | F | 21 | 5 |
| 7 | kaka | 5 | M | 21 | 3 |
| 8 | suke | 3 | M | 20 | 5 |
+----+-------+-----+--------+-----+------+
8 rows in set (0.00 sec)
mysql> select * from courses;
+-----+------------+
| cid | course |
+-----+------------+
| 1 | Chinese |
| 2 | English |
| 3 | math |
| 4 | computer |
| 5 | biological |
+-----+------------+
5 rows in set (0.00 sec)
显示所有同学的选修的第一门课程。
mysql> select student.name,courses.course from student,courses where student.cid = courses.cid;
+-------+------------+
| name | course |
+-------+------------+
| jim | Chinese |
| tom | English |
| lucy | English |
| jack | math |
| neccy | math |
| suke | math |
| mary | computer |
| kaka | biological |
+-------+------------+
8 rows in set (0.00 sec)
mysql> select s.name,c.course from student as s,courses as c where s.cid = c.cid;
+-------+------------+
| name | course |
+-------+------------+
| jim | Chinese |
| tom | English |
| lucy | English |
| jack | math |
| neccy | math |
| suke | math |
| mary | computer |
| kaka | biological |
+-------+------------+
8 rows in set (0.00 sec)
表A... LEFT JOIN 表B... ON ...匹配条件
显示所有学生的第二门选修课程
mysql> select s.name,c.course from student as s left join courses as c on s.cid2 = c.cid;
+-------+------------+
| name | course |
+-------+------------+
| jim | Chinese |
| tom | NULL |
| jack | computer |
| lucy | computer |
| neccy | NULL |
| mary | biological |
| kaka | math |
| suke | biological |
+-------+------------+
8 rows in set (0.00 sec)
表A... RIGHT JOIN 表B... ON ...匹配条件
显示第二门选修课程对应的人
mysql> select s.name,c.course from student as s right join courses as c on s.cid2 = c.cid;
+------+------------+
| name | course |
+------+------------+
| jim | Chinese |
| NULL | English |
| kaka | math |
| jack | computer |
| lucy | computer |
| mary | biological |
| suke | biological |
+------+------------+
7 rows in set (0.00 sec)
cid代表选修科目,cid2代表补考科目,显示选修课挂了的同学。(选修课和补考一样)
mysql> select s.id,s.name,s.cid,c.name,c.cid2 from student as s,student as c where s.cid=c.cid2 and s.name=c.name;
+----+------+-----+------+------+
| id | name | cid | name | cid2 |
+----+------+-----+------+------+
| 1 | jim | 1 | jim | 1 |
+----+------+-----+------+------+
1 row in set (0.00 sec)
一个查询中嵌套另外一个查询语句
查询大于平均年龄的同学
mysql> select name,age from student where age > (select avg(age) from student);
+-------+-----+
| name | age |
+-------+-----+
| lucy | 25 |
| neccy | 30 |
+-------+-----+
2 rows in set (0.00 sec)
显示学生年龄和老师年龄相同的学生
mysql> select name from student where age in (select age from teacher);
+-------+
| name |
+-------+
| lucy |
| neccy |
+-------+
2 rows in set (0.00 sec)
查询学生中大于30岁的人
mysql> select name,age from (select name,age from student) as t where t.age >=30;
+-------+-----+
| name | age |
+-------+-----+
| neccy | 30 |
+-------+-----+
1 row in set (0.00 sec)
mysql> (select name,age from student) union (select name,age from teacher);
+------------+------+
| name | age |
+------------+------+
| jim | 21 |
| tom | 19 |
| jack | 20 |
| lucy | 25 |
| neccy | 30 |
| mary | 21 |
| kaka | 21 |
| suke | 20 |
| yuehan | 25 |
| tomxu | 30 |
| tangyouyou | 50 |
| caiyuanpei | 100 |
+------------+------+
12 rows in set (0.00 sec)
mysql> select * from student;
+----+-------+-----+--------+-----+------+
| id | name | cid | gender | age | cid2 |
+----+-------+-----+--------+-----+------+
| 1 | jim | 1 | M | 21 | 1 |
| 2 | tom | 2 | M | 19 | NULL |
| 3 | jack | 3 | M | 20 | 4 |
| 4 | lucy | 2 | F | 25 | 4 |
| 5 | neccy | 3 | F | 30 | NULL |
| 6 | mary | 4 | F | 21 | 5 |
| 7 | kaka | 5 | M | 21 | 3 |
| 8 | suke | 3 | M | 20 | 5 |
+----+-------+-----+--------+-----+------+
8 rows in set (0.00 sec)
mysql> select * from courses;
+-----+------------+-----+
| cid | course | tid |
+-----+------------+-----+
| 1 | Chinese | 3 |
| 2 | English | 2 |
| 3 | math | 1 |
| 4 | computer | 3 |
| 5 | biological | 4 |
+-----+------------+-----+
5 rows in set (0.00 sec)
mysql> select * from teacher;
+-----+------------+------+
| tid | name | age |
+-----+------------+------+
| 1 | yuehan | 25 |
| 2 | tomxu | 30 |
| 3 | tangyouyou | 50 |
| 4 | caiyuanpei | 100 |
| 5 | hongqigong | 88 |
| 6 | huangrong | 45 |
+-----+------------+------+
6 rows in set (0.00 sec)
mysql> select * from courses where cid not in (select distinct cid2 from student where cid2 is not null);
+-----+---------+
| cid | course |
+-----+---------+
| 2 | English |
+-----+---------+
1 row in set (0.00 sec)
mysql> select course from courses where cid in (select cid from student group by cid having count(cid)>=2);
+---------+
| course |
+---------+
| English |
| math |
+---------+
2 rows in set (0.00 sec)
mysql> select t.name,c.course from teacher as t left join courses as c on c.tid=t.tid;
+------------+------------+
| name | course |
+------------+------------+
| yuehan | math |
| tomxu | English |
| tangyouyou | Chinese |
| tangyouyou | computer |
| caiyuanpei | biological |
| hongqigong | NULL |
| huangrong | NULL |
+------------+------------+
7 rows in set (0.00 sec)
mysql> select t.name,c.course from teacher as t right join courses as c on c.tid=t.tid;
+------------+------------+
| name | course |
+------------+------------+
| tangyouyou | Chinese |
| tomxu | English |
| yuehan | math |
| tangyouyou | computer |
| caiyuanpei | biological |
+------------+------------+
5 rows in set (0.00 sec)
mysql> select obj.name,obj.course,t.name from (select s.name,c.course,c.tid from student as s,courses as c where s.cid=c.cid) as obj,teacher as t where obj.tid = t.tid;
+-------+------------+------------+
| name | course | name |
+-------+------------+------------+
| jack | math | yuehan |
| neccy | math | yuehan |
| suke | math | yuehan |
| tom | English | tomxu |
| lucy | English | tomxu |
| jim | Chinese | tangyouyou |
| mary | computer | tangyouyou |
| kaka | biological | caiyuanpei |
+-------+------------+------------+
8 rows in set (0.00 sec)
mysql> select s.name,c.course,t.name from student as s,courses as c,teacher as t where s.cid=c.cid and c.tid=t.tid;
+-------+------------+------------+
| name | course | name |
+-------+------------+------------+
| jim | Chinese | tangyouyou |
| tom | English | tomxu |
| lucy | English | tomxu |
| jack | math | yuehan |
| neccy | math | yuehan |
| suke | math | yuehan |
| mary | computer | tangyouyou |
| kaka | biological | caiyuanpei |
+-------+------------+------------+
8 rows in set (0.00 sec)