我有兩個表T1和T2。SQL從兩個表中獲取公共行
任何人都可以幫助一個SQL查詢,它將從這兩個表中獲取通用行嗎? (假設T1和T2各有100列)
P.S:我猜INNER JOIN在每一列上都不是一個好主意。
感謝
我有兩個表T1和T2。SQL從兩個表中獲取公共行
任何人都可以幫助一個SQL查詢,它將從這兩個表中獲取通用行嗎? (假設T1和T2各有100列)
P.S:我猜INNER JOIN在每一列上都不是一個好主意。
感謝
select
t1.*
from
t1, t2
where
(
(t1.col1 is null and t2.col1 is null) or (
(t1.col1 = t2.col1 )
) and
(
(t1.col2 is null and t2.col2 is null) or (
(t1.col2 = t2.col2 )
) and
(
(t1.col3 is null and t2.col3 is null) or (
(t1.col3 = t2.col3 )
) and
....
(
(t1.col100 is null and t2.col100 is null) or (
(t1.col100 = t2.col100 )
)
我不是那種在SQL方面經驗豐富的人。所以,如果我們有100列,編寫這個查詢將會非常耗時。不是嗎?我們有其他選擇嗎? – Ram
SELECT * FROM T1
INTERSECT
SELECT * FROM T2
如果您在使用SQL Server 2005,那麼你可以使用相交關鍵詞,它給你共同記錄。
SELECT column1
FROM table1
INTERSECT
SELECT column1
FROM table2
如果您希望在輸出中同時來自兩個表中具有公共列1的表1的column1和column2。
SELECT column1, column2
FROM table1
WHERE column1 IN
(
SELECT column1
FROM table1
INTERSECT
SELECT column1
FROM table2
)
兩張表之間有什麼關係? – HaveNoDisplayName
這些表包含什麼?請發佈完整的數據模式 – Matt
您使用的是什麼RDBMS? – Mureinik