有没有办法检查where-in语句中的表中是否存在特定的元组?
类似于:
create table Test(A int, B int);
insert into Test values (3, 9);
insert into Test values (6, 7);
insert into Test values (7, 6);
insert into Test values (3, 4);
select A, B
from Test
where (B, A) in Test;预期输出:
6|7
7|6发布于 2011-11-07 05:53:54
因此,将Test加入到自身:
select t1.A, t1.B
from Test t1
join Test t2 on t1.A = t2.B and t1.B = t2.A或者使用交叉点:
select A, B from Test
intersect
select B, A from Test不过,自连接可能会更快。
发布于 2011-11-07 05:51:13
你太接近了,"in“子句的后半部分必须是select...所以
SELECT A,B
FROM Test
WHERE (B,A) IN (SELECT B,A FROM Test);测试( in )必须在相同的字段(或字段类型)中
发布于 2011-11-07 06:05:09
当您在数据库的上下文中以这种方式使用单词"tuple“时,您可能会感到困惑,因为单词"tuple”在数据库理论中也有一个正式的定义,这与您的问题中隐含的集合论定义不同。
如果您正在尝试识别不需要的元组,您可以尝试这种方法:
SELECT t1.A, t1.B From Test t1 JOIN Test t2 ON t1.A=t2.B AND t1.B=t2.A WHERE t1.A > t1.Bhttps://stackoverflow.com/questions/8030624
复制相似问题