简体   繁体   English

SQL左连接两个表中的条件

[英]SQL left join with conditions from two tables

Is there a way to combine the following queries Q1 and Q2 into one? 有没有一种方法可以将以下查询Q1和Q2合并为一个?

I believe it to be important to know the structure of the tables a, b & c: 我认为了解表a,b和c的结构很重要:

  • a.ID (auto), a.01 (1 letter) a.ID(自动),a.01(1个字母)
  • b.01 (1 letter), a.02 (1 digit number) b.01(1个字母),a.02(1个数字)
  • c.ID (auto), c.01 (lookup from a.ID), c.02 (1 digit number) c.ID(自动),c.01(从a.ID查找),c.02(1位数字)

All b.01 values will be present in a.01. 所有b.01值都将出现在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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