我有两张桌子,文件和地理信息
docs表:
id category company headorg type
1 1 20 4 aaa
2 1 null 4 bbb
3 null 20 4 ccc
4 null 20 4 ddd
5 2 null 4 bbb
6 null 20 4 ccc地理桌:
id category investor headorg
1 1 20 4
2 1 21 4
3 1 22 4
4 2 21 4
5 2 22 4 现在,我必须根据传递的company=20查询docs表,也必须根据类别检查geo表。
在这里,docs.company只是geo.investor
例如,以类别1为例,在第1类的筛选表中,我们的投资者为20,所以即使公司在docs表中为空,我们也应该获得文档记录1,2。同样,如果公司类别为null,即doc记录为3,4,6
所以我的查询应该返回我1,2,3,4,6
我写了这个但是我得到了所有的记录1,2,3,4,5,6
SELECT * FROM doc d where (company is null or company = 20)
and 20 in ( select geo.investor from geo join doc d on d.category = geo.category)发布于 2018-09-12 20:04:16
我想你想:
SELECT d.*
FROM doc d
WHERE d.company = 20 OR
d.category = (SELECT g.category FROM geo g WHERE g.investor = 20);或者,用JOIN来形容这个词
select d.*
from doc d left join
geo g
on d.category = g.category and g.investor = 20
where d.company = 20 or g.category is not null;https://stackoverflow.com/questions/52302545
复制相似问题