[英]Display results from one table as link to show results to another table?
[英]SQL - show results from table without values from another table
我只需要显示表1和表2中的结果,而不是表3中的结果。 基本上,除了(TABLE1,Table2)和TABLE3之间的INNER JOIN外,它应该类似于TABLE1,Table2。 应该看起来像这样-左侧Table1和Table2,右侧Table3
现在我有这个:
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
// And somehow except values which are in Table3
有人可以帮我吗? 非常感谢。
有两种不同的方法可以做到这一点。 我相信mysql
使用outer join/null
方法会更好:
select t.*
from (
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
) t left join Table3 t3 on t.mesta_email = t3.mesta_email
and t.mesta_kod = t3.mesta_kod
where t3.mesta_email is null
假设table3
与其他2个表具有相同的结构。
在您编写它时,我将使用exists
和not exists
来几乎直接解决该问题:
select t1.mesta_email, t2.mesta_kod
from table1 t1
where exists (select 1
from table2 t2
where t2.mesta_email = t1.mesta_email and t2.mesta_kod = t1.mesta_kod
) and
not exists (select 1
from table3 t3
where t3.mesta_email = t1.mesta_email and t3.mesta_kod = t1.mesta_kod
);
与其他方法相比, exists
/ not exists
一个优点是重复。 如果其中一个表(例如table1
)没有重复项,但其他表可能重复,则无需在结果数据集中删除重复项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.