我有两个表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