一、基本查询
表的增删查改CRUD :Create(创建)、Retrieve(读取)、Update(更新)、Delete(删除)
1.Create
语法:
INSERT INTO table_name
(column \[, column\] ...)
VALUES (value_list) , (value_list) ...
value_list: value, , value ...
案例:创建学生表
mysql> CREATE TABLE students (
-> id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> sn INT NOT NULL UNIQUE COMMENT '学号',
-> name VARCHAR(20) NOT NULL,
-> qq VARCHAR(20)
-> );
Query OK, 0 rows affected (0.11 sec)
(1)插入数据
mysql> insert into students values
(1,100,'xhx','10001') ,
(2,101,'xxz','10002');
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
(2)替换
-- 主键 或者 唯一键 没有冲突,则直接插入;
-- 主键 或者 唯一键 如果冲突,则删除后再插入
mysql> replace into students (sn,name,qq) value(101,'zbr','10003');
Query OK, 2 rows affected (0.02 sec)
mysql> select * from students;
+----+-----+------+-------+
| id | sn | name | qq |
+----+-----+------+-------+
| 1 | 100 | xhx | 10001 |
| 3 | 101 | zbr | 10003 |
+----+-----+------+-------+
2 rows in set (0.00 sec)
mysql> replace into students (sn,name,qq) value(102,'zbr','10003');Query OK, 1 row affected (0.02 sec)
mysql> select * from students;
+----+-----+------+-------+
| id | sn | name | qq |
+----+-----+------+-------+
| 1 | 100 | xhx | 10001 |
| 3 | 101 | zbr | 10003 |
| 4 | 102 | zbr | 10003 |
+----+-----+------+-------+
3 rows in set (0.00 sec)
2.Retrieve
语法:
SELECT
DISTINCT {* | {column , column ...}FROM table_name
WHERE ...
ORDER BY column \[ASC \| DESC\], ...
LIMIT ...
案例
-- 创建表
mysql> CREATE TABLE exam_result (
-> id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20) NOT NULL COMMENT '同学姓名',
-> chinese float DEFAULT 0.0 COMMENT '语文成绩',
-> math float DEFAULT 0.0 COMMENT '数学成绩',
-> english float DEFAULT 0.0 COMMENT '英语成绩'
-> );
Query OK, 0 rows affected (0.09 sec)
-- 插入测试结构
mysql> INSERT INTO exam_result (name, chinese, math, english) VALUES
-> ('唐三藏', 67, 98, 56),
-> ('孙悟空', 87, 78, 77),
-> ('猪悟能', 88, 98, 90),
-> ('曹孟德', 82, 84, 67),
-> ('刘玄德', 55, 85, 45),
-> ('孙权', 70, 73, 78),
-> ('宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
2.1 Select
(1)全列查询
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
(2)指定列查询
mysql> select id,name,english from exam_result ;
+----+-----------+---------+
| id | name | english |
+----+-----------+---------+
| 1 | 唐三藏 | 56 |
| 2 | 孙悟空 | 77 |
| 3 | 猪悟能 | 90 |
| 4 | 曹孟德 | 67 |
| 5 | 刘玄德 | 45 |
| 6 | 孙权 | 78 |
| 7 | 宋公明 | 30 |
+----+-----------+---------+
7 rows in set (0.00 sec)
(3)查询字段为表达式
mysql> select name,chinese+math+english from exam_result;
+-----------+----------------------+
| name | chinese+math+english |
+-----------+----------------------+
| 唐三藏 | 221 |
| 孙悟空 | 242 |
| 猪悟能 | 276 |
| 曹孟德 | 233 |
| 刘玄德 | 185 |
| 孙权 | 221 |
| 宋公明 | 170 |
+-----------+----------------------+
7 rows in set (0.00 sec)
(4)为查询结果指定别名
语法:
SELECT column AS alias_name ... FROM table_name;
例子:
mysql> select name as 名字,chinese+math+english as 总分 from exam_result;
+-----------+--------+
| 名字 | 总分 |
+-----------+--------+
| 唐三藏 | 221 |
| 孙悟空 | 242 |
| 猪悟能 | 276 |
| 曹孟德 | 233 |
| 刘玄德 | 185 |
| 孙权 | 221 |
| 宋公明 | 170 |
+-----------+--------+
7 rows in set (0.00 sec)
(5)结果去重(distinct是针对一行去重)
mysql> select distinct math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.00 sec)
mysql> select math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
7 rows in set (0.00 sec)
2.2 where条件
比较运算符

逻辑运算符

(1)英语不及格的同学及其英语成绩
mysql> SELECT name, english FROM exam_result WHERE english < 60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56 |
| 刘玄德 | 45 |
| 宋公明 | 30 |
+-----------+---------+
3 rows in set (0.00 sec)
(2)语文成绩在 80, 90 分的同学及语文
mysql> select name,chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孙悟空 | 87 |
| 猪悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.01 sec)
(3) 数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
mysql> SELECT name, math FROM exam_result WHERE math = 58 OR math = 59 OR math = 98 OR math = 99;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+------+
2 rows in set (0.00 sec)
(4) 姓孙的同学 及 孙某同学
mysql> select name from exam_result where name like '孙%';
+-----------+
| name |
+-----------+
| 孙悟空 |
| 孙权 |
+-----------+
2 rows in set (0.00 sec)
(5)语文成绩好于英语成绩的同学
mysql> select name,chinese,english from exam_result where chinese>english;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 唐三藏 | 67 | 56 |
| 孙悟空 | 87 | 77 |
| 曹孟德 | 82 | 67 |
| 刘玄德 | 55 | 45 |
| 宋公明 | 75 | 30 |
+-----------+---------+---------+
5 rows in set (0.00 sec)
(6)总分在200分以下的学生
mysql> select name,english+chinese+math total from exam_result where chinese+english+math<200;
+-----------+-------+
| name | total |
+-----------+-------+
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+-------+
2 rows in set (0.00 sec)
(7)语文成绩大于80并且不姓孙
mysql> select name,chinese from exam_result where chinese>80 and name not like '孙%';
+-----------+---------+
| name | chinese |
+-----------+---------+
| 猪悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
2 rows in set (0.00 sec)
(8)孙某同学,否则要求总成绩 > 200 并且 语文成绩 < 数学成绩 并且 英语成绩 > 80
mysql> select *,chinese+english+math from exam_result where name like '孙%' or (chinese+math+english>200 and chinese<math and english>80);
+----+-----------+---------+------+---------+----------------------+
| id | name | chinese | math | english | chinese+english+math |
+----+-----------+---------+------+---------+----------------------+
| 2 | 孙悟空 | 87 | 78 | 77 | 242 |
| 3 | 猪悟能 | 88 | 98 | 90 | 276 |
| 6 | 孙权 | 70 | 73 | 78 | 221 |
+----+-----------+---------+------+---------+----------------------+
3 rows in set (0.00 sec)
(9)NULL查询
mysql> select * from students;
+----+-----+------+-------+
| id | sn | name | qq |
+----+-----+------+-------+
| 1 | 100 | xhx | 10001 |
| 3 | 101 | zbr | 10003 |
| 4 | 102 | zbr | 10003 |
| 5 | 103 | xxz | NULL |
+----+-----+------+-------+
4 rows in set (0.00 sec)
mysql> select * from students where qq is null;
+----+-----+------+------+
| id | sn | name | qq |
+----+-----+------+------+
| 5 | 103 | xxz | NULL |
+----+-----+------+------+
1 row in set (0.00 sec)
null和null的比较
mysql> select null=null,null=1,null=0;
+-----------+--------+--------+
| null=null | null=1 | null=0 |
+-----------+--------+--------+
| NULL | NULL | NULL |
+-----------+--------+--------+
1 row in set (0.00 sec)
mysql> select null<=>null,null<=>1,null<=>0;
+-------------+----------+----------+
| null<=>null | null<=>1 | null<=>0 |
+-------------+----------+----------+
| 1 | 0 | 0 |
+-------------+----------+----------+
1 row in set (0.00 sec)
2.3 结果排序
语法:
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name WHERE ...
ORDER BY column ASC\|DESC, ...;
注意:没有order by 子句的查询,返回的顺序是未定义的,永远不要依靠这个顺序
(1)同学及数学成绩,按数学成绩升序显示
mysql> select name,math from exam_result order by math asc;
+-----------+------+
| name | math |
+-----------+------+
| 宋公明 | 65 |
| 孙权 | 73 |
| 孙悟空 | 78 |
| 曹孟德 | 84 |
| 刘玄德 | 85 |
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+------+
7 rows in set (0.00 sec)
(2)查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
mysql> select * from exam_result order by math desc ,english asc, chinese asc;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
(3)查询同学及总分,由高到低(order by后可以加别名,因为其是在选出表的基础上再进行排序)
mysql> select name,chinese+math+english 总分 from exam_result order by 总分 desc;
+-----------+--------+
| name | 总分 |
+-----------+--------+
| 猪悟能 | 276 |
| 孙悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孙权 | 221 |
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------+
7 rows in set (0.00 sec)
(4)查询姓孙的同学或者姓曹的同学数学成绩,结果按数学成绩由高到低显示(先根据where后的条件查询,结果再进行order排序)
mysql> select name,math from exam_result where name like '孙%' or name like '曹%' order by math desc;
+-----------+------+
| name | math |
+-----------+------+
| 曹孟德 | 84 |
| 孙悟空 | 78 |
| 孙权 | 73 |
+-----------+------+
3 rows in set (0.00 sec)
2.4 筛选分页结果
语法:
-- 起始下标为 0
-- 从 s 开始,筛选 n 条结果
SELECT ... FROM table_name WHERE ... ORDER BY ... LIMIT s, n
-- 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name WHERE ... ORDER BY ... LIMIT n;
;
-- 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
SELECT ... FROM table_name WHERE ... ORDER BY ... LIMIT n OFFSET s;
建议:对于未知表查询,最好加一条limit 1,防止过大,卡死
按id进行分页,每页3条记录,分别显示123页
mysql> select * from exam_result order by id limit 0,3;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
+----+-----------+---------+------+---------+
3 rows in set (0.20 sec)
mysql> select * from exam_result order by id limit 3,3;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result order by id limit 6,3;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)
3.Update
语法:
UPDATE table_name SET column = expr , column = expr ...
WHERE ...\] \[ORDER BY ...\] \[LIMIT ...
对查到的结果进行列值更新
(1) 将孙悟空同学的数学成绩变更为 80 分
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
mysql> update exam_result set math=80 where name='孙悟空';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select name,math from exam_result where name='孙悟空';
+-----------+------+
| name | math |
+-----------+------+
| 孙悟空 | 80 |
+-----------+------+
1 row in set (0.00 sec)
(2) 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
mysql> update exam_result set math=math+30 order by chinese+math+english asc limit 3;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
(3)将所有同学的语文成绩翻倍
mysql> select *,chinese+math+english 总分 from exam_result;
+----+-----------+---------+------+---------+--------+
| id | name | chinese | math | english | 总分 |
+----+-----------+---------+------+---------+--------+
| 1 | 唐三藏 | 67 | 128 | 56 | 251 |
| 2 | 孙悟空 | 87 | 80 | 77 | 244 |
| 3 | 猪悟能 | 88 | 98 | 90 | 276 |
| 4 | 曹孟德 | 82 | 84 | 67 | 233 |
| 5 | 刘玄德 | 55 | 115 | 45 | 215 |
| 6 | 孙权 | 70 | 73 | 78 | 221 |
| 7 | 宋公明 | 75 | 95 | 30 | 200 |
+----+-----------+---------+------+---------+--------+
7 rows in set (0.00 sec)
mysql> update exam_result set chinese=chinese*2;
Query OK, 7 rows affected (0.03 sec)
Rows matched: 7 Changed: 7 Warnings: 0
mysql> select *,chinese+math+english 总分 from exam_result;
+----+-----------+---------+------+---------+--------+
| id | name | chinese | math | english | 总分 |
+----+-----------+---------+------+---------+--------+
| 1 | 唐三藏 | 134 | 128 | 56 | 318 |
| 2 | 孙悟空 | 174 | 80 | 77 | 331 |
| 3 | 猪悟能 | 176 | 98 | 90 | 364 |
| 4 | 曹孟德 | 164 | 84 | 67 | 315 |
| 5 | 刘玄德 | 110 | 115 | 45 | 270 |
| 6 | 孙权 | 140 | 73 | 78 | 291 |
| 7 | 宋公明 | 150 | 95 | 30 | 275 |
+----+-----------+---------+------+---------+--------+
7 rows in set (0.00 sec)
4.Delete
4.1删除数据
语法:
DELETE FROM table_name WHERE ... ORDER BY ... LIMIT ...
(1) 删除孙悟空同学的考试成绩
mysql> delete from exam_result where name='孙悟空';
Query OK, 1 row affected (0.03 sec)
(2)删除整张表数据
mysql> create table for_delete(
-> id int,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.10 sec)
mysql> insert into for_delete value(1,'1'),(2,'2');
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from for_delete;
+------+------+
| id | name |
+------+------+
| 1 | 1 |
| 2 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> delete from for_delete;
Query OK, 2 rows affected (0.02 sec)
mysql> select * from for_delete;
Empty set (0.00 sec)
4.2 截断表
语法:
TRUNCATE TABLE table_name
注意:
- 1.只能对整表使用
- 2.不能像Delete一样针对部分数据
- 3.实际上mysql不对数据操作,比Delete快,但是不能进行回滚
- 4.会重置auto_increment
5.插入查询结果
语法:
INSERT INTO table_name (column \[, column ...)] SELECT ...
案例:删除重复表的重复记录,重复的数据只能有一份
mysql> CREATE TABLE duplicate_table (id int, name varchar(20));
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO duplicate_table VALUES
-> (100, 'aaa'),
-> (100, 'aaa'),
-> (200, 'bbb'),
-> (200, 'bbb'),
-> (200, 'bbb'),
-> (300, 'ccc');
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 100 | aaa |
| 200 | bbb |
| 200 | bbb |
| 200 | bbb |
| 300 | ccc |
+------+------+
6 rows in set (0.00 sec)
mysql> create table new_duplicate_table like duplicate_table;
Query OK, 0 rows affected (0.08 sec)
mysql> insert new_duplicate_table select distinct * from duplicate_table;
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> rename table duplicate_table to old_duplicate_table;
Query OK, 0 rows affected (0.10 sec)
mysql> rename table new_duplicate_table to duplicate_table;
Query OK, 0 rows affected (0.06 sec)
mysql> select * from duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec)
6.聚合函数

6.1 统计班级共有多少同学
mysql> select COUNT(*) from students;
+----------+
| COUNT(*) |
+----------+
| 4 |
+----------+
1 row in set (0.01 sec)
6.2 统计班级收集的qq号有多少(null不会计入结果)
mysql> select COUNT(qq) from students;
+-----------+
| COUNT(qq) |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
6.3 统计本次考试的数学成绩分数的个数
mysql> select COUNT(math) from exam_result;
+-------------+
| COUNT(math) |
+-------------+
| 6 |
+-------------+
1 row in set (0.00 sec)
---COUNT(DISTINCT math)
mysql> select COUNT(distinct math) from exam_result;
+----------------------+
| COUNT(distinct math) |
+----------------------+
| 5 |
+----------------------+
1 row in set (0.00 sec)
6.4 统计数学成绩总分
mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
| 596 |
+-----------+
1 row in set (0.00 sec)
6.5 统计平均分
mysql> select AVG(chinese+math+english) 平均总分 from exam_result;
+--------------+
| 平均总分 |
+--------------+
| 306 |
+--------------+
1 row in set (0.00 sec)
6.6 返回英语最高分
mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
| 90 |
+--------------+
1 row in set (0.00 sec)
6.7 返回>70分以上的数学最低分
mysql> select min(math) from exam_result where math>70;
+-----------+
| min(math) |
+-----------+
| 73 |
+-----------+
1 row in set (0.00 sec)
7.group by子句的使用
在select 中使用group by子句可以对指定列进行分组查询
select column1, column2, .. from table group by column;