从表中显示数据,其中数据不在其他表中,具有分组依据

问题描述 投票:0回答:2

我有一张看起来像这样的桌子。

enter image description here

还有这样一张桌子

enter image description here

我的问题是这个。如何显示table2上不在table1上的数据?以日期为基础。

enter image description here

以及我如何包括日期?

我的意思是什么先生是如何显示table2中不在1 table1中的数据?例如BBB,CCC,DDD,EEE不在表1中仅AAA日期1/1/2018

mysql sql
2个回答
0
投票

使用cross join生成所有可能的行,然后清除不存在的行:

select n.name, d.date
from table2 n cross join
     (select distinct date from table1) d left join
     table1 t1
     on n.name = t1.name and d.date = t1.date
where t1.name is null;

1
投票
SELECT a.*,b.*,
FROM Table1 a
LEFT JOIN Table2 b
Where a.Name <>b.Name

试试上面的查询。如果这不起作用,请提供一个sqlfiddle,我们将改进它。

© www.soinside.com 2019 - 2024. All rights reserved.