我想进行一个查询,从一个表中选择所有行,除了另一个表中与登录用户具有相同 id 并且在另一列中具有相同值的行。
Table1
id | value
=================
1 | 10
2 | 20
3 | 30
4 | 40
5 | 50
6 | 60
——
Table2
id | user_ids | another_column
=========================================
1 | 2 | 30
2 | 4 | 50
3 | 4 | 60
因此,如果 Table2.user_ids = (loggedin userid) 并且同时 Table1.value = Table2.another_column - 这些行不应在结果中输出。
如果我们说我们有一个 id:4 的用户,那么用户不应该看到 Table1 中的 row5 或 row6,因为值匹配
我应该使用某种子查询还是加入 sql?
从上面的示例中获取 id 为 4 的用户:
select * from Table1
where value not in (
select another_column
from Table2
where user_ids = 4
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句