日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQL(二)- 基础查询语句

發布時間:2023/12/4 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL(二)- 基础查询语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單的查詢語句(DQL)

下面我們正式來學習查詢語句,下面所有查詢用到的表均為前面提到的三張表:

員工表中的數據:

部門表中的數據:

薪資表中的數據:

基本查詢語句的語法

select 字段 from 表名;

1.查詢員工的年薪?(字段可以參與數學運算。)

select ename,sal * 12 as '年薪' from emp; //其中as可以省略


注意:標準sql語句中要求字符串使用單引號括起來。雖然mysql支持雙引號,盡量別用

條件查詢語句的語法

select 3字段 from 1表名 where 2條件;

執行順序:先from,然后where,最后select


1.查詢工資等于5000的員工姓名?

select ename from emp where sal = 5000;

2.查詢SMITH的工資?

select sal from emp where ename = 'SMITH'; // 字符串使用單引號括起來。


3.找出工資高于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;

4.找出工資不等于3000的?

select ename,sal from emp where sal <> 3000; select ename,sal from emp where sal != 3000;

5.找出工資在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'; select ename from emp where ename between 'A' and 'D'; // 閉區間。

6.找出哪些人津貼為NULL?
在數據庫當中NULL不是一個值,代表什么也沒有,為空。
空不是一個值,不能用等號衡量。
必須使用 is null或者is not null

select ename,sal,comm from emp where comm is null;select ename,sal,comm from emp where comm = null; //結果:Empty set (0.00 sec)

7.找出哪些人津貼不為NULL?

select ename,sal,comm from emp where comm is not null;

8.找出哪些人沒有津貼?

select ename,sal,comm from emp where comm is null or comm = 0;

9.找出工作崗位是MANAGER和SALESMAN的員工?

select ename,job from emp where job = 'MANAGER' or job = 'SALESMAN';

10.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); // 正確的。

注意:當運算符的優先級不確定的時候加小括號。

11.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后面的值不是區間,是具體的值。


13.not in: 不在這幾個值當中。

select ename,job from emp where sal not in(800, 5000);
模糊查詢like

14.找出名字當中含有O的?
(在模糊查詢當中,必須掌握兩個特殊的符號,一個是%,一個是_)
注意:%代表任意多個字符,_代表任意1個字符。

select ename from emp where ename like '%O%';

15.找出名字中第二個字母是A的?

select ename from emp where ename like '_A%';

16.找出名字中有下劃線的?
新創建的表t_user:

select name from t_user where name like '%\_%'; // 當用到%和_時需要轉譯“\”

注意:當查詢的記錄中包含%和_時需要轉譯“\”

17.找出名字中最后一個字母是T的?

select ename from emp where ename like '%T';

排序查詢(升序、降序)默認升序

排序查詢格式語法

select字段 from表名 where條件 order by字段;

1.按照薪資升序,找出員工姓名和薪資:

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; //指定降序

2.按照工資降序排列,當工資相同按照名字升序排列:

select ename,sal from emp order by sal desc , ename asc;

注意:越靠前的字段越能起到主導作用。只有當前面的字段無法完成排序的時候,才會啟用后面的字段。

3.找出工作崗位是SALESMAN的員工,并且要求按照薪資的降序排列。

select ename,job,sal fromemp where job = 'SALESMAN' order bysal desc;
分組函數/聚合函數/多行函數(自動忽略null)

分組函數通常與group by 聯合使用(having)

  • 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 sum(comm) from emp; select sum(comm) from emp where comm is not null; // 不需要額外添加這個過濾條件。sum函數自動忽略

    1.找出工資高于平均工資的員工:

    select ename,sal from emp where sal > avg(sal); //報錯

    //報錯:ERROR 1111 (HY000): Invalid use of group function

    原因:SQL語句當中有一個語法規則,分組函數不可直接使用在where子句當中。

    正確方法:

    select ename,sal from emp where sal > (select avg(sal) from emp); 怎么解釋?因為group by是在where執行之后才會執行的。

    就是說,都還沒分組,你怎么就執行分組函數呢?
    并且任何一個分組函數(count sum avg max min)都是在group by語句執行結束之后才會執行的。

    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;

    找出工資高于平均工資的員工?
    第一步:找出平均工資

    select avg(sal) from emp;


    第二步:找出高于平均工資的員工

    select ename,sal from emp where sal > 2073.214286;

    將它們嵌套起來使用:

    select ename,sal from emp where sal > (select avg(sal) from emp);
    單行處理函數

    什么是單行處理函數:輸入一行,輸出一行。

    1.計算每個員工的年薪:

    select ename,(sal+comm)*12 as yearsal from emp;

    重點:所有數據庫都是這樣規定的,只要有NULL參與的運算結果一定是NULL。
    2.使用ifnull函數

    select ename,(sal+ifnull(comm,0))*12 as yearsal from emp;


    ifnull() 空處理函數?

    • ifnull(可能含有NULL的字段,被當做什么處理) : 屬于單行處理函數。
    分組查詢(group by 和 having + 分組函數)

    group by : 按照某個字段或者某些字段進行分組。
    having : having是對分組之后的數據進行再次過濾。

    • group by和 having 以及分組函數(count、avg、max、min、sum)常常搭配使用
    • having不能脫離group by單獨使用,having就是為group by服務的

    1.案例:找出每個工作崗位的最高薪資。

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

    注意:

    • 分組函數一般都會和group by聯合使用,這也是為什么它被稱為分組函數的原因。
    • 并且任何一個分組函數(count sum avg max min)都是在group by語句執行結束之后才會執行的。
    • 當一條sql語句沒有group by的話,整張表的數據會自成一組。
    select ename,max(sal),job from emp group by job; //雖然不報錯,但是查詢結果沒意義
    • 以上在mysql當中,查詢結果是有的,但是結果沒有意義,在Oracle數據庫當中會報錯。語法錯誤。

    • Oracle的語法規則比MySQL語法規則嚴謹

    • 記住一個規則:當一條語句中有group by的話,select后面只能跟分組函數和參與分組的字段。

    2.找出每個部門的最高薪資,要求顯示大于2900的數據:

    第一步:找出每個部門的最高薪資:select deptno,max(sal) from emp group by deptno; 第二步:再找出大于2900的數據:select deptno,max(sal) from emp group by deptno having max(sal) >2900;// 這種效率低

    最佳寫法:

    select deptno,max(sal) from emp where sal > 2900 group by deptno; // 這種效率高,建議能夠使用where過濾的盡量使用where

    where搞不定的情況:
    1.找出每個部門的平均薪資,要求顯示薪資大于2000的數據。

    第一步:找出每個部門的平均薪資 select deptno,avg(sal) from emp group by deptno; 第二步:要求顯示薪資大于2000的數據 select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;

    where后面不能使用分組函數:

    select deptno,avg(sal) from emp where avg(sal) > 2000 group by deptno; // 錯誤了。

    這種情況只能使用having過濾。
    原因:因為avg()是分完組后算出來的,max()不是分完組算出來的

    總結DQL語句

    select 5字段 from 1表名 where 2條件 group by 3分組 having 4分組二次過濾 order by 6字段;
    關于查詢結果集的去重

    關鍵字:distinct

    1.查詢所有崗位:

    select distinct job from emp; //distinct 去除重復關鍵字

    2.注意:distinct的前面不能有字段

    select ename distinct job from emp; //報錯,distinct自能出現在所有字段的最前面

    如果distinct 后有多個字段表示,后面所有字段聯合去重。

    3.查看每個部門的不同職位:

    select distinct deptno,job from emp;

    4.統計崗位的數量:

    select count(distinct job) from emp;

    總結

    以上是生活随笔為你收集整理的SQL(二)- 基础查询语句的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。