資料庫第二週學習內容
資料庫第二週學習內容
- 簡單的查詢語句(DQL)
- 條件查詢
- is null
- 模糊查詢like(萬用字元)
- 排序(升序、降序)
- 分組函式
- 單行處理函式
簡單的查詢語句(DQL)
語法格式:
select 欄位名1,欄位名2,欄位名3,… from 表名;
提示:
1、任何一條sql語句以“;”結尾。
2、sql語句不區分大小寫。
查詢員工的年薪?(欄位可以參與數學運算。)
select ename,sal * 12 from emp;
+--------+----------+
| ename | sal * 12 |
+--------+---------- +
| SMITH | 9600.00 |
| ALLEN | 19200.00 |
| WARD | 15000.00 |
| JONES | 35700.00 |
| MARTIN | 15000.00 |
| BLAKE | 34200.00 |
| CLARK | 29400.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
| TURNER | 18000.00 |
| ADAMS | 13200.00 |
| JAMES | 11400.00 |
| FORD | 36000.00 |
| MILLER | 15600.00 |
+--------+----------+
給查詢結果的列重新命名?
select ename,sal * 12 as yearsal from emp;
別名中有中文?
select ename,sal * 12 as 年薪 from emp; // 錯誤
select ename,sal * 12 as '年薪' from emp;
+--------+----------+
| ename | 年薪 |
+--------+----------+
| SMITH | 9600.00 |
| ALLEN | 19200.00 |
| WARD | 15000.00 |
| JONES | 35700.00 |
| MARTIN | 15000.00 |
| BLAKE | 34200.00 |
| CLARK | 29400.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
| TURNER | 18000.00 |
| ADAMS | 13200.00 |
| JAMES | 11400.00 |
| FORD | 36000.00 |
| MILLER | 15600.00 |
+--------+----------+
注意:標準sql語句中要求字串使用單引號括起來。雖然mysql支援雙引號,儘量別用。
as關鍵字可以省略?
> select empno,ename,sal * 12 yearsal from emp;
+-------+--------+----------+
| empno | ename | yearsal |
+-------+--------+----------+
| 7369 | SMITH | 9600.00 |
| 7499 | ALLEN | 19200.00 |
| 7521 | WARD | 15000.00 |
| 7566 | JONES | 35700.00 |
| 7654 | MARTIN | 15000.00 |
| 7698 | BLAKE | 34200.00 |
| 7782 | CLARK | 29400.00 |
| 7788 | SCOTT | 36000.00 |
| 7839 | KING | 60000.00 |
| 7844 | TURNER | 18000.00 |
| 7876 | ADAMS | 13200.00 |
| 7900 | JAMES | 11400.00 |
| 7902 | FORD | 36000.00 |
| 7934 | MILLER | 15600.00 |
+-------+--------+----------+
查詢所有欄位?
select * from emp; // 實際開發中不建議使用*,效率較低。
條件查詢
語法格式:
select
欄位,欄位...
from
表名
where
條件;
執行順序:先from,然後where,最後select
查詢工資等於5000的員工姓名?
select ename from emp where sal = 5000;
+-------+
| ename |
+-------+
| KING |
+-------+
查詢SMITH的工資?
select sal from emp where ename = 'SMITH'; // 字串使用單引號括起來。
+--------+
| sal |
+--------+
| 800.00 |
+--------+
找出工資高於3000的員工?
select ename,sal from emp where sal > 3000;
select ename,sal from emp where sal >= 3000;
select ename,sal from emp where sal < 3000;
select ename,sal from emp where sal <= 3000;
找出工資不等於3000的?
select ename,sal from emp where sal <> 3000;
select ename,sal from emp where sal != 3000;
找出工資在1100和3000之間的員工,包括1100和3000?
select ename,sal from emp where sal >= 1100 and sal <= 3000;
select ename,sal from emp where sal between 1100 and 3000; // between...and...是閉區間 [1100 ~ 3000]
select ename,sal from emp where sal between 3000 and 1100; // 查詢不到任何資料
between and在使用的時候必須左小右大。
between and除了可以使用在數字方面之外,還可以使用在字串方面。
select ename from emp where ename between 'A' and 'C';
+-------+
| ename |
+-------+
| ALLEN |
| BLAKE |
| ADAMS |
+-------+
select ename from emp where ename between 'A' and 'D'; // 左閉右開。
is null
找出哪些人津貼為NULL?
在資料庫當中NULL不是一個值,代表什麼也沒有,為空。
空不是一個值,不能用等號衡量。
必須使用 is null或者is not null
select ename,sal,comm from emp where comm is null;
+--------+---------+------+
| ename | sal | comm |
+--------+---------+------+
| SMITH | 800.00 | NULL |
| JONES | 2975.00 | NULL |
| BLAKE | 2850.00 | NULL |
| CLARK | 2450.00 | NULL |
| SCOTT | 3000.00 | NULL |
| KING | 5000.00 | NULL |
| ADAMS | 1100.00 | NULL |
| JAMES | 950.00 | NULL |
| FORD | 3000.00 | NULL |
| MILLER | 1300.00 | NULL |
+--------+---------+------+
select ename,sal,comm from emp where comm = null;
Empty set (0.00 sec)
找出哪些人津貼不為NULL?
select ename,sal,comm from emp where comm is not null;
+--------+---------+---------+
| ename | sal | comm |
+--------+---------+---------+
| ALLEN | 1600.00 | 300.00 |
| WARD | 1250.00 | 500.00 |
| MARTIN | 1250.00 | 1400.00 |
| TURNER | 1500.00 | 0.00 |
+--------+---------+---------+
找出哪些人沒有津貼?
select ename,sal,comm from emp where comm is null or comm = 0;
+--------+---------+------+
| ename | sal | comm |
+--------+---------+------+
| SMITH | 800.00 | NULL |
| JONES | 2975.00 | NULL |
| BLAKE | 2850.00 | NULL |
| CLARK | 2450.00 | NULL |
| SCOTT | 3000.00 | NULL |
| KING | 5000.00 | NULL |
| TURNER | 1500.00 | 0.00 |
| ADAMS | 1100.00 | NULL |
| JAMES | 950.00 | NULL |
| FORD | 3000.00 | NULL |
| MILLER | 1300.00 | NULL |
+--------+---------+------+
找出工作崗位是MANAGER和SALESMAN的員工?
select ename,job from emp where job = 'MANAGER' or job = 'SALESMAN';
+--------+----------+
| ename | job |
+--------+----------+
| ALLEN | SALESMAN |
| WARD | SALESMAN |
| JONES | MANAGER |
| MARTIN | SALESMAN |
| BLAKE | MANAGER |
| CLARK | MANAGER |
| TURNER | SALESMAN |
+--------+----------+
and和or聯合起來用:找出薪資大於1000的並且部門編號是20或30部門的員工。
select ename,sal,deptno from emp where sal > 1000 and deptno = 20 or deptno = 30; // 錯誤的
select ename,sal,deptno from emp where sal > 1000 and (deptno = 20 or deptno = 30); // 正確的。
注意:當運算子的優先順序不確定的時候加小括號。
and的優先順序比or高
in等同於or:找出工作崗位是MANAGER和SALESMAN的員工?
select ename,job from emp where job = 'SALESMAN' or job = 'MANAGER';
select ename,job from emp where job in('SALESMAN', 'MANAGER');
select ename,job from emp where sal in(800, 5000); // in後面的值不是區間,是具體的值。
+-------+-----------+
| ename | job |
+-------+-----------+
| SMITH | CLERK |
| KING | PRESIDENT |
+-------+-----------+
not in: 不在這幾個值當中。
select ename,job from emp where sal not in(800, 5000);
模糊查詢like
找出名字當中含有O的?
(在模糊查詢當中,必須掌握兩個特殊的符號,一個是%,一個是_)
%代表任意多個字元,_代表任意1個字元。
select ename from emp where ename like '%O%';
+-------+
| ename |
+-------+
| JONES |
| SCOTT |
| FORD |
+-------+
找出名字中第二個字母是A的?
select ename from emp where ename like '_A%';
+--------+
| ename |
+--------+
| WARD |
| MARTIN |
| JAMES |
+--------+
找出名字中有下劃線的?
mysql> select * from t_user;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | WANG_WU |
+------+----------+
select name from t_user where name like '%_%';
+----------+
| name |
+----------+
| zhangsan |
| lisi |
| WANG_WU |
+----------+
select name from t_user where name like '%\_%';
+---------+
| name |
+---------+
| WANG_WU |
+---------+
找出名字中最後一個字母是T的?
select ename from emp where ename like '%T';
+-------+
| ename |
+-------+
| SCOTT |
+-------+
排序(升序、降序)
按照工資升序,找出員工名和薪資?
select
ename,sal
from
emp
order by
sal;
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| JAMES | 950.00 |
| ADAMS | 1100.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN | 1600.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| JONES | 2975.00 |
| FORD | 3000.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
+--------+---------+
注意:預設是升序。怎麼指定升序或者降序呢?asc表示升序,desc表示降序。
select ename , sal from emp order by sal; // 升序
select ename , sal from emp order by sal asc; // 升序
select ename , sal from emp order by sal desc; // 降序。
按照工資的降序排列,當工資相同的時候再按照名字的升序排列。
select ename,sal from emp order by sal desc;
select ename,sal from emp order by sal desc , ename asc;
注意:越靠前的欄位越能起到主導作用。只有當前面的欄位無法完成排序的時候,才會啟用後面的欄位。
找出工作崗位是SALESMAN的員工,並且要求按照薪資的降序排列。
select
ename,job,sal
from
emp
where
job = 'SALESMAN'
order by
sal desc;
+--------+----------+---------+
| ename | job | sal |
+--------+----------+---------+
| ALLEN | SALESMAN | 1600.00 |
| TURNER | SALESMAN | 1500.00 |
| WARD | SALESMAN | 1250.00 |
| MARTIN | SALESMAN | 1250.00 |
+--------+----------+---------+
select
欄位 3
from
表名 1
where
條件 2
order by
.... 4
order by是最後執行的。
分組函式
count 計數
sum 求和
avg 平均值
max 最大值
min 最小值
記住:所有的分組函式都是對“某一組”資料進行操作的。
找出工資總和?
select sum(sal) from emp;
找出最高工資?
select max(sal) from emp;
找出最低工資?
select min(sal) from emp;
找出平均工資?
select avg(sal) from emp;
找出總人數?
select count(*) from emp;
select count(ename) from emp;
分組函式一共5個。
分組函式還有另一個名字:多行處理函式。
多行處理函式的特點:輸入多行,最終輸出的結果是1行。
分組函式自動忽略NULL。
select count(comm) from emp;
+-------------+
| count(comm) |
+-------------+
| 4 |
+-------------+
select sum(comm) from emp;
+-----------+
| sum(comm) |
+-----------+
| 2200.00 |
+-----------+
select sum(comm) from emp where comm is not null; // 不需要額外新增這個過濾條件。sum函式自動忽略NULL。
找出工資高於平均工資的員工?
select avg(sal) from emp; // 平均工資
+-------------+
| avg(sal) |
+-------------+
| 2073.214286 |
+-------------+
select ename,sal from emp where sal > avg(sal); //ERROR 1111 (HY000): Invalid use of group function
思考以上的錯誤資訊:無效的使用了分組函式?
原因:SQL語句當中有一個語法規則,分組函式不可直接使用在where子句當中。why???
怎麼解釋?
因為group by是在where執行之後才會執行的。
select 5
..
from 1
..
where 2
..
group by 3
..
having 4
..
order by 6
..
count()和count(具體的某個欄位),他們有什麼區別?
count():不是統計某個欄位中資料的個數,而是統計總記錄條數。(和某個欄位無關)
count(comm): 表示統計comm欄位中不為NULL的資料總數量。
分組函式也能組合起來用:
select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp;
+----------+----------+-------------+----------+----------+
| count(*) | sum(sal) | avg(sal) | max(sal) | min(sal) |
+----------+----------+-------------+----------+----------+
| 14 | 29025.00 | 2073.214286 | 5000.00 | 800.00 |
+----------+----------+-------------+----------+----------+
找出工資高於平均工資的員工?
第一步:找出平均工資
select avg(sal) from emp;
+-------------+
| avg(sal) |
+-------------+
| 2073.214286 |
+-------------+
第二步:找出高於平均工資的員工
select ename,sal from emp where sal > 2073.214286;
+-------+---------+
| ename | sal |
+-------+---------+
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| FORD | 3000.00 |
+-------+---------+
select ename,sal from emp where sal > (select avg(sal) from emp);
單行處理函式
什麼是單行處理函式?
輸入一行,輸出一行。
計算每個員工的年薪?
select ename,(sal+comm)*12 as yearsal from emp;
重點:所有資料庫都是這樣規定的,只要有NULL參與的運算結果一定是NULL。
使用ifnull函式:
select ename,(sal+ifnull(comm,0))*12 as yearsal from emp;
ifnull() 空處理函式?
ifnull(可能為NULL的資料,被當做什麼處理) : 屬於單行處理函式。
select ename,ifnull(comm,0) as comm from emp;
+--------+---------+
| ename | comm |
+--------+---------+
| SMITH | 0.00 |
| ALLEN | 300.00 |
| WARD | 500.00 |
| JONES | 0.00 |
| MARTIN | 1400.00 |
| BLAKE | 0.00 |
| CLARK | 0.00 |
| SCOTT | 0.00 |
| KING | 0.00 |
| TURNER | 0.00 |
| ADAMS | 0.00 |
| JAMES | 0.00 |
| FORD | 0.00 |
| MILLER | 0.00 |
+--------+---------+