假设我有一个客户端表(tblClients
)和一个引用表(tblQuotes
)。
要获取现有客户的所有报价,我执行了以下操作:
SELECT q.quoteId, c.contact, q.job,
FROM tblQuotes AS q
INNER JOIN tblClients AS c ON q.user = c.user
对于每个报价,我们得到了报价创建日期。该日期位于tblDate
。我找到显示该日期的唯一方法是当我显示我的记录时,我会做另一个这样的请求:
SELECT Date
FROM tblDate
WHERE id = %1 => %1 is q.quoteId
一切正常,但我决定添加交替SQL请求的输入。例如,c.Contact
可以是“John”
WHERE c.Contact = 'John'
Date
怎么样?我可以轻松获得q.quoteId
,c.Contact
和q.Job
,但我也不知道如何使其与Date一起使用。
WHERE c.Contact = 'John' AND ...
答案 0 :(得分:2)
只需添加另一个联接(使用INNER JOIN,假设每个引号都有一个日期值):
SELECT
q.quoteId, c.contact, q.job, d.Date
FROM
tblQuotes AS q INNER JOIN
tblClients AS c ON q.user = c.user INNER JOIN
tblDate AS d on d.id = q.quoteId
然后,您可以相应地进行修改,以使用WHERE
子句指定任何其他条件:
SELECT
q.quoteId, c.contact, q.job, d.Date
FROM
tblQuotes AS q INNER JOIN
tblClients AS c ON q.user = c.user INNER JOIN
tblDate AS d on d.id = q.quoteId
WHERE
d.Contact = 'John'