检查表中是否存在特定元组
有没有办法检查 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
Is there a way to check if a specific tuple exists in a table in a where-in statement?
Something like:
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;
Expected output:
6|7
7|6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你非常接近,“in”子句的后半部分必须是选择...所以
测试(IN)必须位于相同的字段(或字段类型)中
You were super close, the second half of an "in" clause has to be a select... so
The test (IN) must be in the same fields (or types of fields)
因此将
Test
连接到自身:或者使用交集:
不过,自连接可能会更快。
Join
Test
to itself thusly:Or use an intersection:
The self-join would probably be faster though.
当您在数据库上下文中以这种方式使用“元组”一词时,您可能会得到一些困惑,因为“元组”一词在数据库理论中也有一个正式的定义,该定义与您的问题中隐含的集合论定义不同。
如果您尝试识别不需要的元组,您可以尝试以下方法:
You may get some confused looks when you use the word "tuple" in that way in the context of a database, because the word "tuple" also has a formal definition in database theory that is different from the set theory definition implied in your question.
If you're trying to identify unwanted tuples, you could try this approach: