简体   繁体   中英

SQL left join with conditions from two tables

Is there a way to combine the following queries Q1 and Q2 into one?

I believe it to be important to know the structure of the tables a, b & c:

  • a.ID (auto), a.01 (1 letter)
  • b.01 (1 letter), a.02 (1 digit number)
  • c.ID (auto), c.01 (lookup from a.ID), c.02 (1 digit number)

All b.01 values will be present in a.01.

This is Q1:

SELECT a.ID, b.[02], b.[01]
FROM a, b
WHERE (((b.[01])=[a].[01]));

This is Q2:

SELECT Q1.*
FROM Q1 LEFT JOIN c ON [Q1].[ID]=[c].[01]
WHERE ((c.[02]) Is Null);

Something like this might work:

SELECT a.ID, b.[02], b.[01]
FROM a INNER JOIN b ON b.[01] = a.[01]
LEFT JOIN c ON a.id = c.[01]
WHERE c.[02] IS NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM