2.3NULL的查詢
//查詢student表
mysql> select * from student;
+-----+------+-----------+-------+
| id | sn | name | qq |
+-----+------+-----------+-------+
| 100 | 1000 | 唐三藏 | NULL |
| 101 | 1001 | 孫悟空 | 11111 |
| 103 | 2002 | 孫仲謀 | NULL |
| 105 | 2001 | 曹阿瞞 | NULL |
+-----+------+-----------+-------+
//查詢qq號不爲空的同學姓名
mysql> select name,qq from student
-> where qq is not null;
+-----------+-------+
| name | qq |
+-----------+-------+
| 孫悟空 | 11111 |
+-----------+-------+
- null和null的比較
mysql> select null=null,null=1,null=0;
+-----------+--------+--------+
| null=null | null=1 | null=0 |
+-----------+--------+--------+
| NULL | NULL | NULL |
+-----------+--------+--------+
2.4結果排序
語法:
//ASC爲升序(從小到大)
//DESC爲降序(從大到小)
//默認爲ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
案例:
2.4.1查詢同學姓名及數學成績,按照數學成績升序顯示
mysql> select name,shuxue from exam_result
-> order by shuxue;
+-----------+--------+
| name | shuxue |
+-----------+--------+
| 宋公明 | 65 |
| 孫權 | 73 |
| 孫悟空 | 78 |
| 曹孟德 | 84 |
| 劉玄德 | 85 |
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+--------+
2.4.2同學及qq號,按照qq號排序顯示
//null視爲比任何值都要小,升序出現在最上面
mysql> select name,qq from student order by qq;
+-----------+-------+
| name | qq |
+-----------+-------+
| 唐三藏 | NULL |
| 孫仲謀 | NULL |
| 曹阿瞞 | NULL |
| 孫悟空 | 11111 |
+-----------+-------+
2.4.3查詢同學的各門成績,依次按數學降序,英語升序,語文升序的方式顯示
注:多字段排序,排序優先級隨書寫的順序
mysql> select name,shuxue ,yingyu,yuwen from exam_result
-> order by shuxue desc,yingyu,yuwen;
+-----------+--------+--------+-------+
| name | shuxue | yingyu | yuwen |
+-----------+--------+--------+-------+
| 唐三藏 | 98 | 56 | 67 |
| 豬悟能 | 98 | 90 | 88 |
| 劉玄德 | 85 | 45 | 55 |
| 曹孟德 | 84 | 67 | 82 |
| 孫悟空 | 78 | 77 | 87 |
| 孫權 | 73 | 78 | 70 |
| 宋公明 | 65 | 30 | 75 |
+-----------+--------+--------+-------+
2.4.4查詢同學及總分,由高到低
mysql> select name,yuwen+shuxue+yingyu from exam_result
-> order by yuwen+shuxue+yingyu desc;
+-----------+---------------------+
| name | yuwen+shuxue+yingyu |
+-----------+---------------------+
| 豬悟能 | 276 |
| 孫悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孫權 | 221 |
| 劉玄德 | 185 |
| 宋公明 | 170 |
+-----------+---------------------+
order by子句中可以使用列別名
mysql> select name,yuwen+shuxue+yingyu 總分 from exam_result
-> order by 總分 desc;
+-----------+--------+
| name | 總分 |
+-----------+--------+
| 豬悟能 | 276 |
| 孫悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孫權 | 221 |
| 劉玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------+
2.4.5查詢姓孫同學或者姓曹同學的數學成績,結果按照數學成績由高到低顯示
mysql> select name,shuxue from exam_result
-> where name like '孫%' or name like '曹%'
-> order by shuxue desc;
+-----------+--------+
| name | shuxue |
+-----------+--------+
| 曹孟德 | 84 |
| 孫悟空 | 78 |
| 孫權 | 73 |
+-----------+--------+
2.5篩選分頁結果
語法:
-- 從 0 開始,篩選 n 條結果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 從 s 開始,篩選 n 條結果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 從 s 開始,篩選 n 條結果,比第二種用法更明確,建議使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
建議:對未知表進行查詢時,最好加一條
limit 1,避免因爲表中數據過大,查詢全表數據導致數據庫卡死。
案例:按照id分頁,每頁3條記錄,分別顯示第1、2、3頁
//第1頁
mysql> select id,name,shuxue,yuwen,yingyu from exam_result
-> order by id limit 3 offset 0;
+----+-----------+--------+-------+--------+
| id | name | shuxue | yuwen | yingyu |
+----+-----------+--------+-------+--------+
| 1 | 唐三藏 | 98 | 67 | 56 |
| 2 | 孫悟空 | 78 | 87 | 77 |
| 3 | 豬悟能 | 98 | 88 | 90 |
+----+-----------+--------+-------+--------+
//第2頁
mysql> select id,name,shuxue,yuwen,yingyu from exam_result
-> order by id limit 3 offset 3;
+----+-----------+--------+-------+--------+
| id | name | shuxue | yuwen | yingyu |
+----+-----------+--------+-------+--------+
| 4 | 曹孟德 | 84 | 82 | 67 |
| 5 | 劉玄德 | 85 | 55 | 45 |
| 6 | 孫權 | 73 | 70 | 78 |
+----+-----------+--------+-------+--------+
//第3頁
mysql> select id,name,shuxue,yuwen,yingyu from exam_result
-> order by id limit 3 offset 6;
+----+-----------+--------+-------+--------+
| id | name | shuxue | yuwen | yingyu |
+----+-----------+--------+-------+--------+
| 7 | 宋公明 | 65 | 75 | 30 |
+----+-----------+--------+-------+--------+
3.UPDATE
語法:
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]
案例:
3.1將孫悟空同學的數學成績變更爲80分
//查看原數據
mysql> select name,shuxue from exam_result
-> where name='孫悟空';
+-----------+--------+
| name | shuxue |
+-----------+--------+
| 孫悟空 | 78 |
+-----------+--------+
//數據更新
mysql> update exam_result set shuxue=80
-> where name='孫悟空';
//查看更新後的數據
mysql> select name,shuxue from exam_result
-> where name='孫悟空';
+-----------+--------+
| name | shuxue |
+-----------+--------+
| 孫悟空 | 80 |
+-----------+--------+
3.2將曹孟德同學的數學成績變更爲60分,語文變更爲70分(一次更新多列)
//查看原數據
mysql> select name,shuxue,yuwen from exam_result
-> where name='曹孟德';
+-----------+--------+-------+
| name | shuxue | yuwen |
+-----------+--------+-------+
| 曹孟德 | 84 | 82 |
+-----------+--------+-------+
//數據更新
mysql> update exam_result set shuxue=60,yuwen=70
-> where name='曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
//查看更新後的數據
mysql> select name,shuxue,yuwen from exam_result
-> where name='曹孟德';
+-----------+--------+-------+
| name | shuxue | yuwen |
+-----------+--------+-------+
| 曹孟德 | 60 | 70 |
+-----------+--------+-------+
3.3將總成績倒數前三的3位同學的數學成績加上30分。
//查看原數據
mysql> select name,shuxue,yuwen+shuxue+yingyu 總分
-> from exam_result
-> order by 總分 limit 3;
+-----------+--------+--------+
| name | shuxue | 總分 |
+-----------+--------+--------+
| 宋公明 | 65 | 170 |
| 劉玄德 | 85 | 185 |
| 曹孟德 | 60 | 197 |
+-----------+--------+--------+
//數據更新
mysql> update exam_result set shuxue=shuxue+30
-> order by yuwen+shuxue+yingyu limit 3;
//查看更新後的數據,並按照總分升序排序取前3個
mysql> select name,shuxue,yuwen+shuxue+yingyu 總分
-> from exam_result
-> where name in('宋公明','劉玄德','曹孟德');
+-----------+--------+--------+
| name | shuxue | 總分 |
+-----------+--------+--------+
| 曹孟德 | 90 | 227 |
| 劉玄德 | 115 | 215 |
| 宋公明 | 95 | 200 |
+-----------+--------+--------+
3.4將所有同學的語文成績更新爲原來的2倍
//查看原數據
mysql> select name,yuwen from exam_result;
+-----------+-------+
| name | yuwen |
+-----------+-------+
| 唐三藏 | 67 |
| 孫悟空 | 87 |
| 豬悟能 | 88 |
| 曹孟德 | 70 |
| 劉玄德 | 55 |
| 孫權 | 70 |
| 宋公明 | 75 |
+-----------+-------+
7 rows in set (0.00 sec)
//更新數據
mysql> update exam_result set yuwen=yuwen*2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
//查看更新後的數據
mysql> select name,yuwen from exam_result;
+-----------+-------+
| name | yuwen |
+-----------+-------+
| 唐三藏 | 134 |
| 孫悟空 | 174 |
| 豬悟能 | 176 |
| 曹孟德 | 140 |
| 劉玄德 | 110 |
| 孫權 | 140 |
| 宋公明 | 150 |
+-----------+-------+
4.DELETE
4.1刪除數據
語法:
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
4.1.1刪除孫悟空同學的考試成績
//查看原數據
mysql> select * from exam_result where name='孫悟空';
+----+-----------+-------+--------+--------+
| id | name | yuwen | shuxue | yingyu |
+----+-----------+-------+--------+--------+
| 2 | 孫悟空 | 174 | 80 | 77 |
+----+-----------+-------+--------+--------+
1 row in set (0.00 sec)
//刪除數據
mysql> delete from exam_result where name='孫悟空';
Query OK, 1 row affected (0.01 sec)
//查看刪除結果
mysql> select * from exam_result where name='孫悟空';
Empty set (0.00 sec)
4.1.2刪除整張表數據
//建一張表用來刪除
mysql> CREATE TABLE for_delete (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.04 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)
//刪除整張表數據
mysql> delete from for_delete;
Query OK, 3 rows affected (0.00 sec)
//查看刪除結果
mysql> SELECT * FROM for_delete;
Empty set (0.00 sec)
//再插入一條記錄,自增id在原值上增長
mysql> INSERT INTO for_delete (name) VALUES ('D');
Query OK, 1 row affected (0.00 sec)
//查看數據
mysql> SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 4 | D |
+----+------+
4.2截斷表
語法:
TRUNCATE [TABLE] table_name
注意:
- 只能對整張表進行操作,不能向delete一樣對部分數據操作
- 會重置AUTO_INCREMENT項
//建表
mysql> CREATE TABLE for_truncate (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.02 sec)
//插入數據
mysql> INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.00 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)
//截斷整張表,影響行數爲0,所以實際上沒有對數據真正操作
mysql> TRUNCATE for_truncate;
Query OK, 0 rows affected (0.02 sec)
//查看截斷結果
mysql> SELECT * FROM for_truncate;
Empty set (0.00 sec)
//再插入一條數據,自增長id在重新增長
mysql> INSERT INTO for_truncate (name) VALUES ('D');
Query OK, 1 row affected (0.00 sec)
//查看數據
mysql> SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | D |
+----+------+
5.插入查詢結果
語法:
INSERT INTO table_name [(column [, column ...])] SELECT ...
案例:刪除表中的重複記錄,重複的記錄只能有一份
//建原數據表
mysql> CREATE TABLE duplicate_table (id int, name varchar(20));
Query OK, 0 rows affected (0.03 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.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
//創建一張空表no_duplicate_table,結構與duplicate_table一樣
mysql> CREATE TABLE no_duplicate_table LIKE duplicate_table;
Query OK, 0 rows affected (0.02 sec)
//將duplicate_table的去重數據插入到no_duplicate_table
mysql> INSERT INTO no_duplicate_table SELECT DISTINCT * FROM duplicate_table;
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
//通過重命名錶,實現原子的去重操作
mysql> RENAME TABLE duplicate_table TO old_duplicate_table,
-> no_duplicate_table TO duplicate_table;
Query OK, 0 rows affected (0.03 sec)
//查看最終結果
mysql> SELECT * FROM duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
6.聚合函數
6.1常用函數
1).COUNT([DISTINCT] expr) :返回查詢到的數據的數量;
2).SUM([DISTINCT] expr):返回查詢到的數據的總和,不是數字的沒有意義;
3).AVG([DISTINCT] expr):返回查詢到的數據的 平均值,不是數字沒有意義;
4).MAX([DISTINCT] expr):返回查詢到的數據的 最大值,不是數字沒有意義;
5).MIN([DISTINCT] expr):返回查詢到的數據的 最小值,不是數字沒有意義。
6.2案例:
6.2.1統計班級共有多少名學生
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
6.2.2統計班級收集的qq號有多少
mysql> select count(qq) from student;
+-----------+
| count(qq) |
+-----------+
| 1 |
+-----------+
6.2.3統計本次考試的數學成績分數個數
//count(shuxue)統計的是全部成績
mysql> select count(shuxue) from exam_result;
+---------------+
| count(shuxue) |
+---------------+
| 6 |
+---------------+
1 row in set (0.00 sec)
//count(distinct shuxue)統計的是出重成績的數量
mysql> select count(distinct shuxue) from exam_result;
+------------------------+
| count(distinct shuxue) |
+------------------------+
| 5 |
+------------------------+
6.2.4統計所有學生數學成績總和
mysql> select sum(shuxue) from exam_result;
+-------------+
| sum(shuxue) |
+-------------+
| 569 |
+-------------+
6.2.5統計平均總分
mysql> select avg(shuxue+yuwen+yingyu) as 平均總分 from exam_result;
+--------------+
| 平均總分 |
+--------------+
| 297.5 |
+--------------+
6.2.6返回英語最高分
mysql> select max(yingyu) from exam_result;
+-------------+
| max(yingyu) |
+-------------+
| 90 |
+-------------+
1 row in set (0.00 sec)
6.2.7返回>70分以上的數學最低分
mysql> select min(shuxue) from exam_result
-> where shuxue>70;
+-------------+
| min(shuxue) |
+-------------+
| 73 |
+-------------+
7.group by 子句的使用
在select照片那個使用group by子句可以對指定列進行分組查詢
語法:
select column1, column2, .. from table group by column;
7.1如何顯示每個部門的平均工資和最高工資
select deptno,avg(sal),max(sal) from EMP group by deptno;
7.2顯示每個部門的每種崗位的平均工資和最低工資
select avg(sal),min(sal),job, deptno from EMP group by deptno, job;
7.3顯示平均工資低於2000的部門和它的平均工資
select avg(sal) as myavg from EMP group by deptno having myavg<2000;