1. 程式人生 > 其它 >Mysql資料庫複習(1)----單表查詢sql語句

Mysql資料庫複習(1)----單表查詢sql語句

技術標籤:mysql資料庫

寫在最前面:本博文如有錯誤,還望大家指出,謝謝

一、SQL語句

1.SQL語句 的分類

DQL(資料查詢語言):查詢語句(select)
DML(資料操作語言):insert delete update,對錶中的資料進行增刪改
DDL(資料定義語言):create drop alter,對錶結構的增刪改
TCL(事務控制語言):commit提交事務,rollback回滾事務
DCL(資料控制語言):grant授權、revoke撤銷許可權等

2.對sql指令碼的理解

當一個檔案的副檔名是.sql,並且該檔案中編寫了大量的sql語句,我們稱這樣的檔案為sql指令碼

在mysql中可以直接使用source命令執行sql指令碼
sql指令碼中的資料量太大的時候,無法直接開啟,請使用source命令完成初始化

3.刪除資料庫

drop database XXX;

4.條件查詢

語法格式:
select 欄位1,欄位2…
from 表名
where 條件;
先執行from,再執行where,最後執行select
例1:查詢工資等於5000的員工姓名

select name from emptable where sal = 5000;

例2:查詢張三的工資

select sal from emptable where name = ‘張三’;

例3:找出工資高於3000的員工

select name,sal from emptable where sal > 3000;

例4:找出工資不等於3000的員工(兩者均可)

select name,sal from emptable where sal <> 3000;
select name,sal from emptable where sal != 3000;

例5:找出工資在1100和3000之間的員工(包括1100和3000)

select name,sal from emptable where sal >= 1100 and sal <= 3000;
select name,sal from emptable where sal between
1100 and 3000;

注意:between and是閉區間
select name,sal from emptable where sal between 3000 and 1100;
查詢不到任何資料,between and在使用的時候必須左小右大
between and除了可以使用在數字方面之外,還可以使用在字串方面

select name from emptable where name between ‘A’ and ‘B’;

例6:找出哪些人津貼為null
在資料庫中NULL不是一個值,代表什麼也沒有,為空
空不是一個值,不能用等號衡量,必須使用is null 或is not null

select name,sal,comm from emptable where comm is null;

注意這裡不能寫成comm = null
例7:找出哪些人津貼不為NULL

select name,sal,comm from emptable where comm is not null;

例8:找出哪些人沒有津貼(為NULL或者為0)

select name,sal,comm from emptable where comm is null or comm = 0;

例9:找出工作崗位是manager和salesman的員工

select name,job from emptable where job = ‘manager’ or job = ‘salesman’;

例10:找出薪資大於1000的並且部門編號是20或30的員工

select name,sal,depno from emptable where sal > 1000 and (depno = 20 or depno = 30);

and比or的運算子優先順序高,運算子優先順序不確定的時候直接加括號即可
例11:找出工作崗位是manager和salesman的員工

select name,job from emptable where job = ‘manager’ or job = ‘salesman’;

或者

select name,job from emptable where job in(‘salesman’,’manager’);

注意

select name,job from emptable where salin(1000,5000);

找出工資等於1000或工資等於5000的員工
in後面的值不是區間,是具體的值
not in:不在這幾個值裡面

例12:模糊查詢like
a)找出名字中含有O的
兩個特殊的符號:%(任意多個字元),_代表任意一個字元

select name from emptable where name like%O%;

b)找出第二個字母是A的

select name from emptable where name like ‘_A%;

c)找出名字中有下劃線的?(使用轉義字元即可)

select name from emptable where name like%\_%;

5.資料排序(升序或降序)

1)按照工資升序排列?

select name,sal from emptable order by sal;

注意:預設是升序
asc表示升序,desc表示降序

select name,sal from emptable order by sal asc;
select name,sal from emptable order by sal desc;

2)按照工資的降序排列,當工資相同的時候再按照名字的升序排列?

select name,sal from emptable order by sal desc, name asc;

多個欄位排序,越靠前的欄位起的主導作用越大,只有當前面的欄位無法完成排序的時候,後面的欄位才會起作用
3)找出工作崗位是salesman的員工,並要求按照薪資的降序排列

select name,job,sal from emptable where job = ‘salesman’ order by sal desc;

select * from tablename where 條件 order by xx
先執行from,再where,再select,最後執行order by
4)分組函式
count 計數
sum 求和
avg 平均值
max 最大值
min 最小值
所有分組函式都是對某一組資料進行操作的

a)求工資的總和

select sum(sal) from emptable;

b)找出最高/最低/平均工資

select max(sal) from emptable;
select min(sal) from emptable;
select avg(sal) from emptable;

c)找出總人數

select count(*) from emptable;

注意:分組函式自動忽略NULL
所有資料庫都規定,只要有NULL參與的運算,結果就為NULL
ifnull ( ) 空處理函式?
格式:
ifnull(可能為NULL的資料,被當作什麼處理)

d)如果津貼為NULL,就當作0處理

select name,ifnull(comm,0) as coom from emptable;

注意:所有分組函式自動忽略NULL
e)找出工資高於平均工資的員工?

select name,sal from emptable where sal > avg(sal);

以上程式碼是錯誤的
SQL語句中有一個語法規則,分組函式不可出現在where子句中
原因:因為group by是在where執行之後才會執行的,而分組函式是在group by執行之後才執行
正確寫法:

select name,sal from emptable where sal > (select avg(sal) from emptable);

count()與count(某個具體欄位)的區別:
count(
):統計總記錄條數
count(XX):表示統計XX欄位中不為NULL的元素的總數量
分組函式也可以組合使用:

select count(*),sum(sal),avg(sal) from emptable;

5)group by 和 having
group by:按照某個欄位或者某些欄位進行分組
having:對分組之後的資料進行再次過濾

找出每個工作崗位的最高薪資?

select max(sal),job from emp group by job;

分組函式一般都會和group by聯合使用,且分組函式是在group by語句執行結束後執行
當一條sql語句沒有group by的時候,整張表的資料會自成一組
各個語句執行順序如下

在這裡插入圖片描述

注意:當一條語句中有group by的話select後面只能跟分組函式和參與分組的欄位
多個欄位聯合分組
找出每個部門不同工作崗位的最高薪資

select deptno,job,max(sal) from emptable group by deptno,job;

having
找出每個部門的最高薪資,要求顯示薪資大於2900的資料

select max(sal),deptno from emptable group by deptno having max(sal) > 2900;

或者

select max(sal),deptno from emptable where sal > 2900 group by deptno;

6.查詢結果集的去重

加上distinct關鍵字即可

select distinct job from emptable;

注意:distinct關鍵字只能出現在所有欄位的最前面

select distinct deptno,job from emptable;

這裡表示deptno和job聯合去重
案例:統計崗位的數量?

select count(distinct job) from emp;

本筆記網課資料來源:

https://www.bilibili.com/video/BV1fx411X7BD?p=25