SQL Server 2008 R2:查找 column1 中存在 column2 值的行
SQL Server 2008 R2: Find rows where column2 value present in column1
我想找到 col1 中存在 col2 记录的条目。
Table:
CREATE TABLE Test_Table
(
Col1 int,
Col2 int
);
条目:
INSERT INTO Test_Table VALUES(111,112),
(112,113),
(114,115),
(116,117),
(117,118),
(118,119);
预期结果:
Col1 Col2
-------------
111 112
112 113
116 117
117 118
118 119
注意:记录 114,115
未显示,因为 115
不存在于 col1
。
我的尝试:
WITH CTE1
AS
(
SELECT Col1, Col2
FROM Test_Table
),
CTE2
AS
(
SELECT t.Col1, t.Col2
FROM Test_Table t
INNER JOIN CTE1 s1
ON s1.Col2 = t.Col1
OR s1.Col2 = t.Col2
)
SELECT DISTINCT * FROM CTE2;
但是正在获取所有记录。
我想你只是想要 exists
:
select tt.*
from test_table tt
where exists (select 1
from test_table tt2
where tt2.col1 = tt.col2
);
使用CROSS JOIN
:
select t1.* from test_table t1 CROSS JOIN test_table t2
on t1.col1 = t2.col2
UNION
select t1.* from test_table t1 CROSS JOIN test_table t2
on t1.col2 = t2.col1
根据您的评论,我怀疑您希望 col1/col2 存在于另一行的 col1/col2
select tt.*
from test_table tt
where exists (select 1
from test_table tt2
where -- Ensure not same row
(tt2.col1 <> tt.col1 or tt2.col2 <> tt.col2)
and -- Ensure at least one match
(tt2.col1 = tt.col1 or tt2.col1 = tt.col2 or tt2.col2 = tt.col1 or tt2.col2 = tt.col2)
);
我想这就是你想要的。
select t.*
from #test_table t
where exists (select 1
from #test_table t2
where t2.col1 = t.col2
)
or exists (select 1
from #test_table t3
where t3.col2 = t.col1
);
select t1.* from Test_Table t1 left join Test_Table t2 on t2.Col1=t1.Col2 where t2.Col2 is not null
union
select t1.* from Test_Table t1 left join Test_Table t2 on t2.Col2=t1.Col1 where t2.Col1 is not null
这可能也有效
select t.*
from Test_Table t
where exists (select 1
from test_table t2
where t2.col1 = t.col2 or t2.Col2 = t.Col1
)
我想找到 col1 中存在 col2 记录的条目。
Table:
CREATE TABLE Test_Table
(
Col1 int,
Col2 int
);
条目:
INSERT INTO Test_Table VALUES(111,112),
(112,113),
(114,115),
(116,117),
(117,118),
(118,119);
预期结果:
Col1 Col2
-------------
111 112
112 113
116 117
117 118
118 119
注意:记录 114,115
未显示,因为 115
不存在于 col1
。
我的尝试:
WITH CTE1
AS
(
SELECT Col1, Col2
FROM Test_Table
),
CTE2
AS
(
SELECT t.Col1, t.Col2
FROM Test_Table t
INNER JOIN CTE1 s1
ON s1.Col2 = t.Col1
OR s1.Col2 = t.Col2
)
SELECT DISTINCT * FROM CTE2;
但是正在获取所有记录。
我想你只是想要 exists
:
select tt.*
from test_table tt
where exists (select 1
from test_table tt2
where tt2.col1 = tt.col2
);
使用CROSS JOIN
:
select t1.* from test_table t1 CROSS JOIN test_table t2
on t1.col1 = t2.col2
UNION
select t1.* from test_table t1 CROSS JOIN test_table t2
on t1.col2 = t2.col1
根据您的评论,我怀疑您希望 col1/col2 存在于另一行的 col1/col2
select tt.*
from test_table tt
where exists (select 1
from test_table tt2
where -- Ensure not same row
(tt2.col1 <> tt.col1 or tt2.col2 <> tt.col2)
and -- Ensure at least one match
(tt2.col1 = tt.col1 or tt2.col1 = tt.col2 or tt2.col2 = tt.col1 or tt2.col2 = tt.col2)
);
我想这就是你想要的。
select t.*
from #test_table t
where exists (select 1
from #test_table t2
where t2.col1 = t.col2
)
or exists (select 1
from #test_table t3
where t3.col2 = t.col1
);
select t1.* from Test_Table t1 left join Test_Table t2 on t2.Col1=t1.Col2 where t2.Col2 is not null
union
select t1.* from Test_Table t1 left join Test_Table t2 on t2.Col2=t1.Col1 where t2.Col1 is not null
这可能也有效
select t.*
from Test_Table t
where exists (select 1
from test_table t2
where t2.col1 = t.col2 or t2.Col2 = t.Col1
)