语法:
INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...
案例:
CREATE TABLE students (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
sn INT NOT NULL UNIQUE COMMENT '学号',
name VARCHAR(20) NOT NULL,
qq VARCHAR(20)
);
mysql> insert into students values(1,123,'张三','21241040305');
Query OK, 1 row affected (0.01 sec)
mysql> insert into students values(2,234,'李四','21241040306');
Query OK, 1 row affected (0.01 sec)
mysql> select* from students;
+----+-----+--------+-------------+
| id | sn | name | qq |
+----+-----+--------+-------------+
| 1 | 123 | 张三 | 21241040305 |
| 2 | 234 | 李四 | 21241040306 |
+----+-----+--------+-------------+
2 rows in set (0.00 sec)
mysql> insert into students (sn,name,qq) values(235,'王五','21241040307'),(236,'赵六','21241040308');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select* from students;
+----+-----+--------+-------------+
| id | sn | name | qq |
+----+-----+--------+-------------+
| 1 | 123 | 张三 | 21241040305 |
| 2 | 234 | 李四 | 21241040306 |
| 3 | 235 | 王五 | 21241040307 |
| 4 | 236 | 赵六 | 21241040308 |
+----+-----+--------+-------------+
4 rows in set (0.00 sec)
由于主键或者唯一键对应的值已经存在而导致插入失败,所以可以选择性的进行同步更新操作。
语法:
INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...
案例:
mysql> insert into students (sn,name,qq) values(333,'李麻子','21241040333')
-> on duplicate key update sn=236,name='赵六';
Query OK, 1 row affected (0.02 sec)
mysql> select* from students;
+----+-----+-----------+-------------+
| id | sn | name | qq |
+----+-----+-----------+-------------+
| 1 | 123 | 张三 | 21241040305 |
| 2 | 234 | 李四 | 21241040306 |
| 3 | 235 | 王五 | 21241040307 |
| 4 | 236 | 赵六 | 21241040308 |
| 5 | 333 | 李麻子 | 21241040333 |
+----+-----+-----------+-------------+
5 rows in set (0.00 sec)
mysql> insert into students (sn,name,qq) values(444,'老王','21241040334') on duplicate key update sn=777,name='鸡哥';
Query OK, 1 row affected (0.00 sec)
mysql> select* from students;
+----+-----+-----------+-------------+
| id | sn | name | qq |
+----+-----+-----------+-------------+
| 1 | 123 | 张三 | 21241040305 |
| 2 | 234 | 李四 | 21241040306 |
| 3 | 235 | 王五 | 21241040307 |
| 4 | 236 | 赵六 | 21241040308 |
| 5 | 333 | 李麻子 | 21241040333 |
| 6 | 444 | 老王 | 21241040334 |
+----+-----+-----------+-------------+
6 rows in set (0.00 sec)
此时由于没有冲突数据,所以李麻子和老王被成功插入,假设现在插入一个sn与老王冲突会发生什么?
mysql> insert into students (sn,name,qq) values(444,'鸡哥','21241040339') on duplicate key update sn=777,name='鸡哥';
Query OK, 2 rows affected (0.00 sec)
mysql> select* from students;
+----+-----+-----------+-------------+
| id | sn | name | qq |
+----+-----+-----------+-------------+
| 1 | 123 | 张三 | 21241040305 |
| 2 | 234 | 李四 | 21241040306 |
| 3 | 235 | 王五 | 21241040307 |
| 4 | 236 | 赵六 | 21241040308 |
| 5 | 333 | 李麻子 | 21241040333 |
| 6 | 777 | 鸡哥 | 21241040334 |
+----+-----+-----------+-------------+
6 rows in set (0.00 sec)
我们发现老王的数据被鸡哥所顶替,但是要注意只是顶替了老王得sn和name,由于我们并没有修改qq号,所以鸡哥是可以使用老王得qq号的。
大家再看看下面这种方式:
mysql> insert into students (sn,name,qq) values(333,'苏珊','21241040339') on duplicate key update sn=250,name='梅狸猫';
Query OK, 2 rows affected (0.01 sec)
mysql> select* from students;
+----+-----+-----------+-------------+
| id | sn | name | qq |
+----+-----+-----------+-------------+
| 1 | 123 | 张三 | 21241040305 |
| 2 | 234 | 李四 | 21241040306 |
| 3 | 235 | 王五 | 21241040307 |
| 4 | 236 | 赵六 | 21241040308 |
| 5 | 250 | 梅狸猫 | 21241040333 |
| 6 | 777 | 鸡哥 | 21241040334 |
+----+-----+-----------+-------------+
6 rows in set (0.00 sec)
由于新插入的数据sn与李麻子冲突了,所以会修正,但是大家注意看我们values中想插入的是苏珊,但是我们在 update后面是这样写的: sn=250,name=‘梅狸猫’;而最终结果会以update的更新为主。
总结:
-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新
案例:
mysql> replace into students (sn,name) values(999,'狮豹者');
Query OK, 1 row affected (0.00 sec)
mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql> replace into students (sn,name) values(236,'你干嘛!');
Query OK, 2 rows affected (0.00 sec)
mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)
这个与前面讲解的效果基本一致,不过这种方式使用起来要方便些。
语法:
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.03 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.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
通常情况下不建议使用 *
进行全列查询
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> select name,math,chinese from exam_result;
+-----------+------+---------+
| name | math | chinese |
+-----------+------+---------+
| 唐三藏 | 98 | 67 |
| 孙悟空 | 78 | 87 |
| 猪悟能 | 98 | 88 |
| 曹孟德 | 84 | 82 |
| 刘玄德 | 85 | 55 |
| 孙权 | 73 | 70 |
| 宋公明 | 65 | 75 |
+-----------+------+---------+
7 rows in set (0.00 sec)
mysql> select name,chinese+100 from exam_result;
+-----------+-------------+
| name | chinese+100 |
+-----------+-------------+
| 唐三藏 | 167 |
| 孙悟空 | 187 |
| 猪悟能 | 188 |
| 曹孟德 | 182 |
| 刘玄德 | 155 |
| 孙权 | 170 |
| 宋公明 | 175 |
+-----------+-------------+
7 rows in set (0.00 sec)
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)
mysql> select name,chinese+math+english total from exam_result;
+-----------+-------+
| name | total |
+-----------+-------+
| 唐三藏 | 221 |
| 孙悟空 | 242 |
| 猪悟能 | 276 |
| 曹孟德 | 233 |
| 刘玄德 | 185 |
| 孙权 | 221 |
| 宋公明 | 170 |
+-----------+-------+
7 rows in set (0.00 sec)
mysql> select distinct math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.00 sec)
比较运算符:
运算符 | 说明 |
---|---|
>, >=, <, <= | 大于,大于等于,小于,小于等于 |
= | 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL |
<=> | 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) |
!=, <> | 不等于 |
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) |
mysql> select name,english from exam_result where english<60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56 |
| 刘玄德 | 45 |
| 宋公明 | 30 |
+-----------+---------+
3 rows in set (0.00 sec)
mysql> select name,chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孙悟空 | 87 |
| 猪悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
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)
mysql> select name,math from exam_result where math in(58,59,98,99);
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+------+
2 rows in set (0.00 sec)
姓孙的同学:
mysql> select name from exam_result where name like '孙%';
+-----------+
| name |
+-----------+
| 孙悟空 |
| 孙权 |
+-----------+
2 rows in set (0.00 sec)
孙某同学:
mysql> select name from exam_result where name like '孙_';
+--------+
| name |
+--------+
| 孙权 |
+--------+
1 row in set (0.00 sec)
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)
mysql> select name,chinese+english+math total from exam_result where chinese+math+english>200;
+-----------+-------+
| name | total |
+-----------+-------+
| 唐三藏 | 221 |
| 孙悟空 | 242 |
| 猪悟能 | 276 |
| 曹孟德 | 233 |
| 孙权 | 221 |
+-----------+-------+
5 rows in set (0.00 sec)
思考下下面这种查询为什么失败?
mysql> select name,chinese+english+math total from exam_result where total>200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
我们画张图来解释:
在查询时我们第一步肯定要做的事情是在哪一个表查询,第二步就要找到查询条件,也就是where后面的字段,最后一步才是我们想要以什么格式显现出来。所以由于我们指定别名是在查询条件后面,所以查询条件必须得一个一个写,where
后面不能够使用别名。
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)
mysql> select name from exam_result where (name like '孙_') or (english+chinese+math>200 and chinese<math and english>80);
+-----------+
| name |
+-----------+
| 猪悟能 |
| 孙权 |
+-----------+
2 rows in set (0.00 sec)
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)
ASC
为升序(从小到大)DESC
为降序(从大到小)语法:
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
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)
mysql> select name,math,english,chinese from exam_result order by math desc,english asc,chinese asc;
+-----------+------+---------+---------+
| name | math | english | chinese |
+-----------+------+---------+---------+
| 唐三藏 | 98 | 56 | 67 |
| 猪悟能 | 98 | 90 | 88 |
| 刘玄德 | 85 | 45 | 55 |
| 曹孟德 | 84 | 67 | 82 |
| 孙悟空 | 78 | 77 | 87 |
| 孙权 | 73 | 78 | 70 |
| 宋公明 | 65 | 30 | 75 |
+-----------+------+---------+---------+
7 rows in set (0.00 sec)
mysql> select name,chinese+math+english total from exam_result order by chinese+math+english desc;
+-----------+-------+
| name | total |
+-----------+-------+
| 猪悟能 | 276 |
| 孙悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孙权 | 221 |
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+-------+
7 rows in set (0.00 sec)
大家想想为什么此时使用别名能够正常运行了呢?
mysql> select name,chinese+math+english total from exam_result order by total desc;
+-----------+-------+
| name | total |
+-----------+-------+
| 猪悟能 | 276 |
| 孙悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孙权 | 221 |
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+-------+
7 rows in set (0.00 sec)
我们同样画张图来理解:
前面三步依旧是老样子不变,后面第四步为什么是order by子句呢?大家一定要清楚我们在进行排序前一定是已经有了数据,也就是说数据得准备好,而步骤三就是准备了哪些数据,所以order by
语句是可以使用别名的。
语法:
-- 起始下标为 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;
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s,n;
案例:
mysql> select name,chinese,math,english from exam_result order by math+chinese+english desc limit 3 offset 0;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 猪悟能 | 88 | 98 | 90 |
| 孙悟空 | 87 | 78 | 77 |
| 曹孟德 | 82 | 84 | 67 |
+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select name,chinese,math,english from exam_result order by math+chinese+english desc limit 3 offset 3;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 唐三藏 | 67 | 98 | 56 |
| 孙权 | 70 | 73 | 78 |
| 刘玄德 | 55 | 85 | 45 |
+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select name,chinese,math,english from exam_result order by math+chinese+english desc limit 3 offset 6;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 宋公明 | 75 | 65 | 30 |
+-----------+---------+------+---------+
1 row in set (0.00 sec)
除此之外我们还可以使用下面这种方式:
mysql> select name,chinese,math,english from exam_result order by math+chinese+english desc limit 0,3;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 猪悟能 | 88 | 98 | 90 |
| 孙悟空 | 87 | 78 | 77 |
| 曹孟德 | 82 | 84 | 67 |
+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select name,chinese,math,english from exam_result order by math+chinese+english desc limit 3,3;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 唐三藏 | 67 | 98 | 56 |
| 孙权 | 70 | 73 | 78 |
| 刘玄德 | 55 | 85 | 45 |
+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select name,chinese,math,english from exam_result order by math+chinese+english desc limit 6,3;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 宋公明 | 75 | 65 | 30 |
+-----------+---------+------+---------+
1 row in set (0.00 sec)
语法:
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
先准备一张测试表:
mysql> CREATE TABLE for_delete (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
删除整张表(注意删除的是表里面的数据,表本身还在)
当我们再插入一条数据,自增 id 在原值上增长:
语法:
TRUNCATE [TABLE] table_name
注意:这个操作慎用。
先准备一张测试表;
mysql> CREATE TABLE for_truncate (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
截断整表数据:
语法:
INSERT INTO table_name [(column [, column ...])] SELECT ...
案例:删除表中的的重复复记录,重复的数据只能有一份。
创建原数据表;
插入测试数据;
创建一张空表 no_duplicate_table结构,和 duplicate_table 一样;
将 duplicate_table 的去重数据插入到 no_duplicate_table;
通过重命名表,实现原子的去重操作;
查看最终结果。