插入
1.insert
語法:
INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...
- 案例
先創建一張學生表
mysql> create table student(
-> id int unsigned primary key auto_increment,
-> sn int not null unique comment '學號',
-> name varchar(20) not null,
-> qq varchar(20));
1.1單行插入+全列插入
mysql> insert into student values(100,1000,'唐三藏',null);
mysql> insert into student VALUES (101, 1001, '孫悟空', '11111');
//查看插入的結果
mysql> select * from student;
+-----+------+-----------+-------+
| id | sn | name | qq |
+-----+------+-----------+-------+
| 100 | 1000 | 唐三藏 | NULL |
| 101 | 1001 | 孫悟空 | 11111 |
+-----+------+-----------+-------+
1.2多行插入+指定列插入
mysql> insert into student (id,sn,name) values
-> (102,2001,'曹孟德'),
-> (103,2002,'孫仲謀');
mysql> select * from student;
+-----+------+-----------+-------+
| id | sn | name | qq |
+-----+------+-----------+-------+
| 100 | 1000 | 唐三藏 | NULL |
| 101 | 1001 | 孫悟空 | 11111 |
| 102 | 2001 | 曹孟德 | NULL |
| 103 | 2002 | 孫仲謀 | NULL |
+-----+------+-----------+-------+
注意:value_list 的數量和順序與指定列的數量和順序必須一致。
1.3插入否則更新記錄
由於主鍵或者唯一值對應的值已經存在而導致插入失敗
//主鍵衝突導致插入失敗
mysql> insert into student(id,sn,name) values(100,1010,'唐xx');
ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'
//唯一鍵衝突導致插入失敗
mysql> insert into student(sn,name) values(2001,'曹xx');
ERROR 1062 (23000): Duplicate entry '2001' for key 'sn'
1.4替換
主鍵或者唯一鍵衝突,刪除後再插入
mysql> REPLACE INTO student(sn, name) VALUES (2001, '曹阿瞞');
Query OK, 2 rows affected (0.01 sec)
//2 rows affected表示表中有衝突數據,刪除後重新插入
1.5插入有要求
insert into:插入數據,如果主鍵重複,則報錯
insert repalce:插入替換數據,如果存在主鍵或unique數據則替換數據
insert or ignore:如果存在數據,則忽略。
2.查詢
語法:
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 '同學姓名',
-> yuwen float DEFAULT 0.0 COMMENT '語文成績',
-> shuxue float DEFAULT 0.0 COMMENT '數學成績',
-> yingyu float DEFAULT 0.0 COMMENT '英語成績');
mysql> INSERT INTO exam_result (name, yuwen, shuxue, yingyu) VALUES
-> ('唐三藏', 67, 98, 56),
-> ('孫悟空', 87, 78, 77),
-> ('豬悟能', 88, 98, 90),
-> ('曹孟德', 82, 84, 67),
-> ('劉玄德', 55, 85, 45),
-> ('孫權', 70, 73, 78),
-> ('宋公明', 75, 65, 30);
2.1SELECT列
2.1.1全列查詢
mysql> select * from exam_result;
+----+-----------+-------+--------+--------+
| id | name | yuwen | shuxue | yingyu |
+----+-----------+-------+--------+--------+
| 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 |
+----+-----------+-------+--------+--------+
2.1.2指定列查詢
mysql> select id,name,yingyu from exam_result;
+----+-----------+--------+
| id | name | yingyu |
+----+-----------+--------+
| 1 | 唐三藏 | 56 |
| 2 | 孫悟空 | 77 |
| 3 | 豬悟能 | 90 |
| 4 | 曹孟德 | 67 |
| 5 | 劉玄德 | 45 |
| 6 | 孫權 | 78 |
| 7 | 宋公明 | 30 |
+----+-----------+--------+
2.1.3查詢字段爲表達式
//給所有人英語成績加10分
mysql> select id,name,yingyu+10 from exam_result;
+----+-----------+-----------+
| id | name | yingyu+10 |
+----+-----------+-----------+
| 1 | 唐三藏 | 66 |
| 2 | 孫悟空 | 87 |
| 3 | 豬悟能 | 100 |
| 4 | 曹孟德 | 77 |
| 5 | 劉玄德 | 55 |
| 6 | 孫權 | 88 |
| 7 | 宋公明 | 40 |
+----+-----------+-----------+
//表達式包含多個字段
//查詢所有人的總成績
mysql> select id,name,yuwen+shuxue+yingyu from exam_result;
+----+-----------+---------------------+
| id | name | yuwen+shuxue+yingyu |
+----+-----------+---------------------+
| 1 | 唐三藏 | 221 |
| 2 | 孫悟空 | 242 |
| 3 | 豬悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 劉玄德 | 185 |
| 6 | 孫權 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+---------------------+
2.1.4爲查詢結果指定別
語法:
SELECT column [AS] name [...] FROM table_name;
mysql> select id,name,yuwen+shuxue+yingyu as 總分 from exam_result;
+----+-----------+--------+
| id | name | 總分 |
+----+-----------+--------+
| 1 | 唐三藏 | 221 |
| 2 | 孫悟空 | 242 |
| 3 | 豬悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 劉玄德 | 185 |
| 6 | 孫權 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------+
2.1.5查詢結果去重
//數學成績裏98分重複
mysql> select shuxue from exam_result;
+--------+
| shuxue |
+--------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+--------+
//去重之後的結果
mysql> select distinct shuxue from exam_result;
+--------+
| shuxue |
+--------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+--------+
2.2WHERE條件
2.2.1比較運算符:
1)>,>=,<,<=(大於,大於等於,小於,小於等於)
2)!=,<>不等於
3)BETWEEN a0 AND a1(範圍匹配,[a0,a1])
4)IN(option,……)如果是option中的任意一個,返回TRUE(1)
5)IS NULL,IS NOT NULL
6)LIKE模糊匹配(%表示任意多個字符;_表示任意一個字符)
2.2.2運算符
1)AND:多個條件都爲true,結果才爲true
2)OR:任意一個條件爲true,結果爲true
3)NOT:條件爲true,結果爲false
案例:
2.2.3英語不及格的同學及英語成績
mysql> select name,yingyu from exam_result where yingyu<60;
+-----------+--------+
| name | yingyu |
+-----------+--------+
| 唐三藏 | 56 |
| 劉玄德 | 45 |
| 宋公明 | 30 |
+-----------+--------+
2.2.4語文成績在[80,90]分的同學及語文成績
mysql> select name,yuwen from exam_result where yuwen between 80 and 90;
+-----------+-------+
| name | yuwen |
+-----------+-------+
| 孫悟空 | 87 |
| 豬悟能 | 88 |
| 曹孟德 | 82 |
+-----------+-------+
2.2.5查詢數學成績是58或者59或者99或者98分的同學及數學成績
mysql> select name,shuxue from exam_result
-> where shuxue=58 or shuxue=59 or shuxue=90
-> or shuxue=98 or shuxue=99;
+-----------+--------+
| name | shuxue |
+-----------+--------+
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+--------+
2 rows in set (0.00 sec)
或者使用IN條件
mysql> select name,shuxue from exam_result where shuxue IN(58,59,98,99);
+-----------+--------+
| name | shuxue |
+-----------+--------+
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+--------+
2.2.6姓孫的同學及孫某同學
mysql> SELECT name FROM exam_result WHERE name LIKE '孫%';
//或者使用_字符
mysql> SELECT name FROM exam_result WHERE name LIKE '孫_';
+-----------+
| name |
+-----------+
| 孫悟空 |
| 孫權 |
+-----------+
+-----------+
| name |
+-----------+
| 孫悟空 |
| 孫權 |
+-----------+
2.2.7語文成績比英語成績的同學
mysql> SELECT name, yuwen, yingyu FROM exam_result WHERE yuwen > yingyu;
+-----------+-------+--------+
| name | yuwen | yingyu |
+-----------+-------+--------+
| 唐三藏 | 67 | 56 |
| 孫悟空 | 87 | 77 |
| 曹孟德 | 82 | 67 |
| 劉玄德 | 55 | 45 |
| 宋公明 | 75 | 30 |
+-----------+-------+--------+
2.2.8總分在200分以下的同學
mysql> select id,name,yuwen+shuxue+yingyu as 總分 from exam_result
-> where yuwen+shuxue+yingyu <200;
+----+-----------+--------+
| id | name | 總分 |
+----+-----------+--------+
| 5 | 劉玄德 | 185 |
| 7 | 宋公明 | 170 |
+----+-----------+--------+
2.2.9語文成績>80且不姓孫的同學
mysql> select name,yuwen from exam_result
-> where yuwen> 80 and name not like '孫%';
+-----------+-------+
| name | yuwen |
+-----------+-------+
| 豬悟能 | 88 |
| 曹孟德 | 82 |
+-----------+-------+
2.2.10孫某同學,否則要求總成績>200並且 語文成績<數學成績並且英語成績>80
mysql> SELECT name, yuwen, shuxue, yingyu, yuwen + shuxue + yingyu 總分
-> FROM exam_result
-> WHERE name LIKE '孫_' OR (
-> yuwen + shuxue + yingyu > 200 AND yuwen < shuxue AND yingyu > 80
-> );
+-----------+-------+--------+--------+--------+
| name | yuwen | shuxue | yingyu | 總分 |
+-----------+-------+--------+--------+--------+
| 豬悟能 | 88 | 98 | 90 | 276 |
| 孫權 | 70 | 73 | 78 | 221 |
+-----------+-------+--------+--------+--------+