SQL怎麼查詢兩個表中不同的資料

2022-02-04 12:37:36 字數 5614 閱讀 2656

1樓:幻翼高達

我們需要準備的材料分別是:電腦、sql查詢器。

1、首先,開啟sql查詢器,連線上相應的資料庫表,以查詢c1表和c2表的name欄位不同為例。

2、點選「查詢」按鈕,輸入:

select c1.`name` from c1 left join c2 on c1.`name`=c2.`name` where c2.`name` is null

union select c2.`name` from c2 left join c1 on c1.`name`=c2.

`name` where c1.`name` is null。

3、點選「執行」按鈕,此時不同的name都被查詢出了。

2樓:幻翼高達

sql查詢兩個表中不同資料的步驟如下:

我們需要準備的材料分別是:電腦、sql查詢器。

1、首先,開啟sql查詢器,連線上相應的資料庫表,以查詢c1表和c2表的name欄位不同為例。

2、點選「查詢」按鈕,輸入:

select c1.`name` from c1 left join c2 on c1.`name`=c2.`name` where c2.`name` is null

union select c2.`name` from c2 left join c1 on c1.`name`=c2.

`name` where c1.`name` is null。

3、點選「執行」按鈕,此時不同的name都被查詢出了。

3樓:狒狒

1,關鍵是第一步,你要找出表1中有,而表2中沒有的資料行.,select t1.* from 表1 t1

left join 表2 t2 on t1.?=t2.?

2, 以什麼列相同判定為有,都在這裡做等於where isnull(t2.?,'nodata')='nodata'

3,如果表1有而表2也有的列,為null則說明未匹配上,這個列的值不能為null,可以預設為空,否則無法區為值為null或因為匹配不成功為null

【sql】

sql語言,是結構化查詢語言(structured query language)的簡稱。sql語言是一種資料庫查詢和程式設計語言,用於存取資料以及查詢、更新和管理關聯式資料庫系統;同時也是資料庫指令碼檔案的副檔名。

sql怎麼查詢兩個表中不同的資料 5

4樓:

工具/材料:management studio。

1、首先在桌面上,點選「management studio」圖示。

2、其次在該介面中,點選「新建查詢」按鈕。

3、繼續在該介面中,輸入查詢兩個表中不同的資料的sql語句。

4、再者在該介面中,點選「執行」按鈕。

5、最後在該介面中,顯示兩個表中不同的資料。

5樓:風翼殘念

使用except函式,select * from b where (select count(1) from a where a.id = b.id) = 0.

方法一(推薦)

with   c as ( select   name

where    not exists ( select 1

where  b.name = a.name

group by b.name )

group by a.name

select  count(1)

from    c

方法二with    c as ( select   a.name

group by a.name

except

select   b.name

group by b.name

select  count(1)

from    c

方法三select  count(a.name)

where   b.id is null

擴充套件資料:

高階查詢運算詞:

a: union 運算子:

union 運算子通過組合其他兩個結果表(例如 table1 和 table2)並消去表中任何重複行而派生出一個

結果表。當 all 隨 union 一起使用時(即 union all),不消除重複行。兩種情況下,派生表的每一行

不是來自 table1 就是來自 table2。

b: except 運算子

except 運算子通過包括所有在 table1 中但不在 table2 中的行並消除所有重複行而派生出一個結果表。當 all 隨 except 一起使用時 (except all),不消除重複行。

c: intersect 運算子

intersect 運算子通過只包括 table1 和 table2 中都有的行並消除所有重複行而派生出一個結果表。當

all 隨 intersect 一起使用時 (intersect all),不消除重複行。

注:使用運算詞的幾個查詢結果行必須是一致的。

6樓:sql的藝術

假設兩個表都有唯一鍵userid

可以這麼寫(使用全連線【full outer join】:完整外部聯接返回左表和右表中的所有行。當某行在另一個表中沒有匹配行時,則另一個表的選擇列表列包含空值。

如果表之間有匹配行,則整個結果集行包含基表的資料值。)

select

*from

rcsa_userinfodel a

full outer join rcsa_userinfo b on a.userid=b.userid

where

a.userid is null

or b.userid is null

7樓:匿名使用者

sql server 2005之後有自帶的函式可以處理這個功能,用except可以處理

select * from a

except

select * from b

可以參考

8樓:匿名使用者

用minus語句可以實現,就是

select * from a minus select * from b

9樓:匿名使用者

select a.* from a where a.產權證號 not in (select b.產權證號 from b)

10樓:微風

在sql server中

select * from a

except

select * from b

sql查詢兩個表相同的兩個欄位裡不同的資料有哪些

11樓:幸運的

sql語句如下:

select * from table1

full join table2 on  table1.xingming = table2.xingming

where

table1.xingming is null or table2.xingming is null

分析:1、首先得出兩個表的並集

注:full join :存在匹配,匹配顯示;同時,將各個表中不匹配的資料與空資料行匹配進行顯示。可以看成是左外連線與右外連線的並集。

圖中結果左側兩列為table1,右側兩列為table2。

前三條記錄表示table1和table2都有的資料。

table1項為null的記錄說明table2中無相同項。

同理,table2項為null的記錄說明table1中無相同項。

下面,只需要設定篩選條件,過濾出所需記錄。

2、設定過濾條件,得到結果

從結果中可以看出,表1中的趙二在表2中沒有相同xingming的記錄。

表2中的劉六在表1中沒有相同xingming的記錄。

本題還有其它多種解法,此處列出比較好理解的一種。

12樓:匿名使用者

select * from table1 minus select * from table2

union all

select * from table2 minus select * from table1

原理minus : 返回第一個表中有、第二個表中沒有的資料注意:

minus 是 oracle 裡面用的。

如果是 sql server 的話, 用 except 替換掉 minus.

13樓:匿名使用者

easy

select xingming from table1 where not exists (select 1 from table2 where xingming = table1.xingming)

union

select xingming from table2 where not exists (select 1 from table1 where xingming = table2.xingming)

14樓:笑年

select *

from table1

where table1.xingming not in (select * from table2)

union

select *

from table2

where table2.xinming not in (select * from table1)

15樓:匿名使用者

select xingming from table1 where not exists (select 1 from table2 where xingming = table1.xingming)

union

select xingming from table2 where not exists (select 1 from table1 where xingming = table2.xingming)

16樓:匿名使用者

select * from table1 where xingming not in(select xingming from table2)

17樓:綠蔥蔥

select xingming from table1 where xingming='趙二'

select xingming from table1 where xingming='馬七'

select xingming from table2 where xingming='劉六'

sql語句如何查詢兩個表中幾個欄位有不相同的資料集合?

18樓:

select * from table1 a where not exists (select 1 from table2 b where a.a <> b.a and a.

b<>b.b and a.c <> b.c)

sql兩個表多列聯合查詢,sql語句 同時查詢兩個表

select a.b.備註 from select 姓名,一班,一班 as 班級 from 課程 表 union all select 姓名,二班,二班 as 班級 from 課程 表 union all select 姓名,三班,三班 as 班級 from 課程 表 a left join 流水錶...

sql根據欄位不同值查詢不同表中的欄位

select case role when 0 then select name from tablea where dd 11 else select name from tableb where dd 11 end as name from post 就是使用 case 我沒有實驗過。如果有心在...

sql,表與表之間列的包含查詢,sql中引用一個表的查詢結果作為條件來查詢另一個表如何實現?

具體什麼資料庫?最後你要的資料什麼樣子?sql語句如何模糊查詢兩個表中兩列的包含情況 50 select from 表名 where col1 like convert nvarchar,select col2 from dbo.userinfo where 條件 注意 因為 like 這裡面的模糊...