關於SQL中的Except

來源:互聯網
上載者:User

標籤:

 1 use test1 2 select address_district,COUNT(address_district) as total from hr_personal_test group by address_district with rollup; 3 create table test1 (id int); 4 create table test2 (num int); 5  6 --1 1(1,2,3) 2(1,4) 7 --insert into test1 select 1 union select 2 union select 3 8 --insert into test2 select 1 union select 4 9 10 --select id from test1 except select num from test2;-------返回結果2,311 12 13 delete from test1 ;14 delete from test2;15 --2 1(1,2) 2(1,2,3,4)16 --insert into test1 select 1 union select 2 17 --insert into test2 select 1 union select 2 union select 3 union select 418 --select id from test1 except select num from test2; --------返回結果 沒有19 20 delete from test1 ;21 delete from test2;22 --3 1(1,2) 2(3,4) 23 insert into test1 select 1 union select 2 24 insert into test2 select 3 union select 425 select id from test1 except select num from test2-------- 返回結果 1,226 27 delete from test1 ;28 delete from test2;29 --4 1(1,2,3) 2(1,2)30 insert into test1 select 1 union select 2 union select 331 insert into test2 select 1 union select 2 32 select id from test1 except select num from test2--------返回結果3

 

create table test1(id int ,name nvarchar(20))create table test2(id int ,post nvarchar(4))insert into test1 (name) values (‘a‘),(‘b‘),(‘b‘),(‘c‘)insert into test2 (post) values(‘a‘)select name from test1 except select post from test2--------返回 b,cselect name from test1 where name not in(select post from test2)---------返回 b,b,c

 

由此可以得出以下幾點:

1、Except運算子對比的是資料,而非按照不同表中相同欄位名的欄位。

2、Except運算子簡而言之:尋找左邊表中不在右邊表中存在的資料。用集合來表示應該是 A-A交B 而非 (A並B)-(A交B)。

3、Except運算子會去重,而not in 不會。

關於SQL中的Except

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.