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

2021-03-29 00:25:00 字數 2661 閱讀 2598

1樓:幸運的

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的記錄。

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

2樓:匿名使用者

select * from table1 minus select * from table2

union all

select * from table2 minus select * from table1

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

minus 是 oracle 裡面用的。

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

3樓:匿名使用者

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)

4樓:笑年

select *

from table1

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

union

select *

from table2

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

5樓:匿名使用者

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)

6樓:匿名使用者

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

7樓:綠蔥蔥

select xingming from table1 where xingming='趙二'

select xingming from table1 where xingming='馬七'

select xingming from table2 where xingming='劉六'

sql語句如何查詢乙個表中某兩個欄位的相同資料?

8樓:

除重select distinct a.id as aid,a.name as aname,b.

id as bid,b.name as bname from a inner join b on(a.name=b.

name and a.id=b.id)

不除重select a.id as aid,a.name as aname,b.

id as bid,b.name as bname from a inner join b on(a.name=b.

name and a.id=b.id)

9樓:匿名使用者

select * from a

inner join b on a.name = b.name and a.id = b.id

where a.name = '張三' and a.id = '008'

內連線即可

10樓:輕癮

select name,id,count(*) from a group by name,id

11樓:青龍襲天

select * from a where a.name=b.name and a.id=b.id

應該是這個了吧

sql兩表聯合查詢「根據這兩個字段值相同查詢其他字段值」怎

通過在關聯的時抄候,強制指定排襲序規則,bai來避免衝突。select a.file1 as a1,b.file1 as b1 from a join b on a.file1 b.file1 collate chinese prc cs as sql語言du 是結構化查zhi詢語言 structu...

前面兩個字相同後面兩個字相同的詞有哪些

丁丁列列 丁丁噹噹 七七八八 萬萬千千 三三兩兩 三三五五 三三四四 上上下下 世世代代 世世生生 業業兢兢 業業矜矜 丟丟秀秀 兩兩三三 嚴嚴實實 嚴嚴翼翼 豐豐滿滿 樂樂醄醄 樂樂陶陶 乒桌球乓 乾乾翼翼 亮亮堂堂 親親切切 親親熱熱 許許多多,快快樂樂,平平安安,兢兢業業,恭恭敬敬,嗚嗚咽咽,...

情侶網名兩個字。兩個字兩個字必須兩個字。男生要起得高冷一點

男生 爵爺 女生 嬌嬌 冷冷的笑 不過是為了掩飾心痛 冷冷的看 不過是為了離痛遠點 你好,很高興為你解答 得意時淡然 失意時坦然 希望能夠幫助你,滿意請採納 兩個字的情侶網名,正常一點的。20 魑魅,魍魎 寶塔,河妖 不離,不棄 若隱,若現 綠葉,紅花 杯子,開水 冰箱,凍梨 高富帥,白富美 火鍋,...