[英]SQL select records
i have two tables 我有两张桌子
--table a--
first second
------ -------
user-1 user-2
user-2 user-1
user-2 user-3
--table b--
ignoreby ignored
-------- -------
user-1 user-2
user-1 user-4
user-3 user-4
i want to select the records from table a which are not ignoring themself(including unidirectional records) on table b 我想从表a中选择不忽略它们的记录(包括单向记录)
The result here would be: user-2 user-3 结果将是:user-2 user-3
The record user-2 user-1 should not be included in the result because user-1 ignores user-2 and this is unidirectional. 记录中的user-2 user-1不应包含在结果中,因为user-1会忽略user-2且这是单向的。
Thanks 谢谢
Do you mean "bidirectional" and not "unidirectional"? 您是说“双向”而不是“单向”吗? If so:
如果是这样的话:
select a.*
from tablea a
where not exists (select 1
from tableb b
where (b.ignoreby = a.first and b.ignored = a.second) or
(b.ignoreby = a.second and b.ignored = a.first)
);
If you really do mean "unidirectional", then you would only use the first condition. 如果确实表示“单向”,那么您将只使用第一个条件。
Maybe you can try a different and more simpler approach. 也许您可以尝试其他更简单的方法。 Like adding a column called is_ignored with a boolean value to your first table (Table a).
就像在第一个表(表a)中添加一个带有布尔值的is_ignored列一样。 Then you can directly see with a single select if the user ignored the other user.
然后,您可以通过一次选择直接查看该用户是否忽略了另一个用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.