1. 程式人生 > 其它 >MySQL資料庫之——高階SQL語句(二)VIEW檢視、聯集和常見計算

MySQL資料庫之——高階SQL語句(二)VIEW檢視、聯集和常見計算

一、VIEW(檢視)

可以被當做是虛擬表或儲存查詢

檢視跟表格的不同是,表格中有實際儲存資料,而檢視是建立在表格之上的一個架構,它本身並不實際儲存資料。

臨時表在使用者退出或同資料庫的連線斷開後就自動消失了,而檢視不會消失。

檢視不含有資料,只儲存它的定義,它的用途一般可以簡化複雜的查詢。比如你要對幾個表進行連線查詢,而且還要進行統計排序等操作,寫SQL語句會很麻煩的,用檢視將幾個表聯結起來,然後對這個檢視進行查詢操作,就和對一個表查詢一樣,很方便。

檢視本身

2、建立、檢視和刪除檢視

CREATE VIEW "視圖表名" AS "SELECT 語句";          #建立視圖表
SELECT * FROM `V_NAME_VALUE`;                   #檢視視圖表
DROP VIEW V_NAME_VALUE;                         #刪除視圖表
select A.name,A.age from test6 A where name in (select B.name from test1 B where age >19 and age < 23);
create view v_test1_6 as select A.name,A.age from test6 A where name in (select B.name from test1 B where age >19 and age < 23);
select * from v_test1_6;
drop view v_test1_6;

二、聯集

將兩個SQL語句的結果合併起來,兩個SQL語句所產生的欄位需要是同樣的資料種類

1、UNION

生成結果的資料值將沒有重複,且按照欄位的順序進行排序

[SELECT 語句 1] UNION [SELECT 語句 2];

2、UNION ALL

將生成結果的資料值都列出來,無論有無重複

[SELECT 語句 1] UNION ALL [SELECT 語句 2];

三、交集值

取兩個SQL語句結果的交集

1、取交集值的方法1(2種簡單方法,內連線+on/using 去重則加上distinct)

select A.name from test1 A inner join test6 B on A.name=B.name;
select A.name from test1 A inner join test6 B using(name);
select distinct A.name from test1 A inner join test6 B on A.name=B.name;

2、取交集方法2(1種,union all 結合 group by)

兩表其中的一個表沒有指定的行,而另一個表這個行有重複不可用,要求兩個表確實有交集的時候用

select A.name from (select name from test1 union all select name from test7) A group by A.name having count(*) > 1;
select A.name from (select name from test1 union all select name from test7) A group by A.name having count(name) > 1; #不去重顯示<br>
select name from test1 union all select name from test7; #拆分上面的SQL語句
select A.name,count(name) from (select name from test1 union all select name from test7) A group by A.name having count(name) > 1; #顯示count值,便於理解
 
select A.name,count(name) from (select distinct name from test1 union all select distinct name from test7) A group by A.name having count(name) >1;  #去重顯示,在聯集兩個表之前先把表去重,以防一個表中本身就有重複值

3、取交集(去重)——4種方法

取兩個SQL語句結果的交集,且沒有重複

方法一:
select A.name from (select B.name from test1 B inner join test6 C on B.name=C.name) A group by A.name;
select B.name from test1 B inner join test6 C on B.name=C.name;
select * from test1 B inner join test6 C on B.name=C.name;<br>
方法二:
select distinct A.name from test1 A inner join test6 B using(name);<br>
方法三:
select distinct name from test1 where name in (select name from test6);<br>
方法四:
select distinct A.name from test1 A left join test6 B using(name) where B.name is NOT NULL;
select distinct A.name from test1 A left join test6 B using(name);
select distinct A.name,B.name,B.age from test1 A left join test6 B using(name);

3.1 內連線取交集結合group by 去重

3.2內連線取交集結合distinct去重

3.3 where+in 遍歷取交集並結合distinct去重

3.4 使用左連線(也可用右連線)+where 判斷NOT NULL 取交集並結合distinct去重

四、無交集值

顯示第一個SQL語句的結果,且與第二個SQL語句沒有交集的結果,且沒有重複

方法一:
select A.name from (select distinct name from test1 union all select distinct name from test6) A group by A.name having count(name)=1;<br>
方法二:
select distinct name from test1 where name not in (select name from test6);
select distinct name from test6 where name not in (select distinct name from test1);<br>
方法三:
select distinct A.name from test1 A left join test6 B using(name) where B.name is NULL;
select distinct B.name from test1 A right join test6 B using(name) where A.name is NULL;

五、CASE的用法

是SQL用來作為IF-THEN-ELSE之類邏輯的關鍵字

1、語法格式

SELECT CASE (欄位名)
    WHEN "條件1" THEN  "結果1"
    WHEN "條件2" THEN  "結果2"
    ……
    ELSE "結果N"
    END
FROM "表名"

條件可以是一個數值或是公式。ELSE子句不是必須 的

2、示例

mysql> select case name           #選擇欄位name
    -> when 'lili' then age + 2   #當name=lili,則age的值+2
    -> when 'kate' then age - 2   #當name=kate,則age的值-2
    -> when 'lucy' then age * 2   #當name=lucy,則age的值*2
    -> else age / 2               #其他值,則age的值/2
    -> end 'new age',            #結束case,並命名新的age值
    -> name,age from test1;      #選擇要顯示的欄位以及要指定的表

六、排名的計算

表格自我連線(self join),然後將結果依序列出,算出每一行之前(包括那一行本身)有多少行數

select A1.name,A1.age,count(A1.age) from test1 A1,test1 A2 where A1.age < A2.age or (A1.age=A2.age and A1.name=A2.name) group by A1.name order by count(A1.age);
 
select A1.name,A1.age,count(A1.age) rank from test1 A1,test1 A2 where A1.age < A2.age or (A1.age=A2.age and A1.name=A2.name) group by A1.name order by rank;

註釋:統計age欄位的值是比自己本身的值小的以及age欄位和name欄位都相同的數量,比如lisi為6+1=7

七、中位數的計算

方法一:使用派生表
select name,age from ( select A1.name,A1.age,count(A1.age) rank from test1 A1,test1 A2 where A1.age < A2.age or (A1.age=A2.age and A1.name=A2.name) group by A1.name order by rank) A3 where A3.rank = (select (count(*)+1) DIV 2 from test1);
 
方法二:使用視圖表
create view v_rank as select A1.name,A1.age,count(A1.age) rank from test1 A1,test1 A2 where A1.age < A2.age or (A1.age=A2.age and A1.na.name=A2.name) group by A1.name order by rank;
select name,age 'middle age' from v_rank v_rank where rank=(select (count(*)+1) DIV 2 from v_rank;

註釋:每個派生表必須有自己的別名,所以別名A3必須有

DIV是在MySQL中算出商的方式

八、累積總計的計算

表格自我連線(self join),然後將結果依序列出,算出每一行之前(包括那一行本身)的總和

select A1.name,A1.age,sum(A2.age) "total age",count(A1.age) rank from test1 A1,test1 A2 where A1.age < A2.age or (A1.age=A2.age and A1.name=A2.name) group by A1.name order by rank;

九、總合百分比的計算

每個值佔總和的比例

第一步:算出所佔總合比(小數形式)
select A1.name,A1.age,A1.age/(select sum(age) from test1) per_age,count(A1.age) rank from test1 A1,test1 A2 where A1.age < A2.age or (A1.age=A2.age and A1.name=A2.name) group by A1.name order by rank;
第二步:使用round四捨五入取2位小數,並乘以100
select A1.name,A1.age,round(A1.age/(select sum(age) from test1)*100,2) per_age,count(A1.age) rank from test1 A1,test1 A2 where A1.age e < A2.age or (A1.age=A2.age and A1.name=A2.name) group by A1.name order by rank;
第三步:使用|| 拼接%,得出百分比
select A1.name,A1.age,round(A1.age/(select sum(age) from test1)*100,2) || '%' per_age,count(A1.age) rank from test1 A1.test1 A2 where e A1.age < A2.age or (A1.age=A2.age and A1.name=A2.name) group by A1.name order by rank;

十、空值(NUL)和無值(“”)的區別

區別:

無值的長度為0,不佔用空間;而空值null 的長度是null,是佔用空間的;

IS NULL或者IS NOT NULL,是用來判斷欄位是不是NULL或者不是NULL,是不能查出是不是無值的;

無值的判斷使用=’‘或者<>’'來處理。<>代表不等於;

在通過count()指定欄位統計又多少行數時,如果遇到NULL值會自動忽略掉,遇到空值會自動加入記錄中進行計算。

1、判斷空值和無值的字元長度

select length(NULL),length(''),length('1');

2、判斷並查詢空值和非空值

select id,name from test1 where name is NULL;
select id,name from test1 where name is not NULL;

3、判斷並查詢無值和非無值

select id,name from test1 where name='';
select id,name from test1 where name <> '';

4、使用count統計行數(體現null與空值的區別)

count(*)表示包括所有列的行數,不會忽略null值;空值正常統計

count(列名)表示只包括這一列,統計時會忽略null值的行;空值正常統計

select id,name from test1;
select count(*) from test1;
select count(name) from test1;