显示来自第四个表中不存在的三个不同表的列

时间:2017-04-15 17:55:29

标签: sql

TableDates
================
- mDate

tblLocations
================
- location_id
- location

tblSignature_labels
===================
- signature_label_id
- signature_label
- isrequired bit

tblsignatures
====================
- location_id
- signature_label_id
- signature_date

我需要显示tblsignatures表中tblsignatures.signature_date = tblDates.mdate以及tblSignature_labels.isrequired=1tblLocations.location_id=tblSignatures.location_id

中不存在的日期,位置和signature_label

因此,如果需要其中一个signature_labels并且location_id和该位置的日期不在签名表中,请列出不在签名表中的日期,位置和签名标签...清除为泥吧?

任何人都可以帮助兄弟在这里:)

1 个答案:

答案 0 :(得分:0)

使用cross join生成前三个表的所有组合。然后是一些更多逻辑(例如left join)来排除已填充的逻辑:

select d.mdate, l.location_id, sl.signature_label
from tableDates d cross join
     tblLocations l cross join
     tblSignature_labels sl left join
     tblsignatures s
     on s.location_id = l.location_id and
        s.signature_label_id = sl.signature_label_id and
        s.signature_date = d.mDate
where s.location_id is null;