1. 程式人生 > 其它 >Mac切換到指定輸入法

Mac切換到指定輸入法

目錄

一、VIEW(檢視)

1、 概念

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

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

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

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

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

CREATE VIEW "視圖表名" AS "SELECT 語句";                      #建立視圖表
SELECT * FROM `V_NAME_VALUE`;                       #檢視視圖表
DROP VIEW V_NAME_VALUE;                         #刪除視圖表

例項操作:

create view v_test1_2 as select A.name,A.xueke from test2 A where name in (select B.name from test1 B where age > 20);
select *from v_test1_2;
drop view v_test1_2;

二、聯集

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

1、UNION

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

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

例項操作:

select name from test1 union select name from test2;


 

2、UNION ALL

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

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

例項操作:

select name from test1 union all select name from test2;


  

三、交集值

取兩個SQL語句結果的交集

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

select A.name from test1 A inner join test2 B on A.name=B.name;
select A.name from test1 A inner join test2 B using(name);
select distinct A.name from test1 A inner join test2 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 test2) A group by A.name having count(*) > 1;
select A.name from (select name from test1 union all select name from test2) A group by A.name having count(name) > 1; 
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 test2) 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 test2) A group by A.name having count(name) > 1; #去重顯示,在聯集兩個表之前先把表去重,以防一個表中本身就有重複值


  

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

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

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

方法一:內連線取交集結合group by去重


  

方法二:內連線取交集結合distinct去重

方法三:where+in遍歷取交集並結合distinct去重


 

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


  

四、無交集值

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

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

方法一:union all結合group by進行分組彙總並使用count=1取無交集值

方法二:where+not in遍歷取無交集值並結合distinct去重

方法三:使用左連線(或者右連線)+where 判斷NULL 取無交集並結合distinct去重

五、CASE的用法

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

1、語法格式:

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

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

2、例項操作


 

六、空值(NULL)和無值(“”)的區別

區別:

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

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

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

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

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

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

  

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

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

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