0

假设我有一张客户表 ( 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可以是“约翰”

WHERE c.Contact = 'John'

怎么样Date?我可以很容易地得到q.quoteId,但我不知道如何让它与 Date 一起工作c.Contactq.Job

WHERE c.Contact = 'John' AND ...
4

1 回答 1

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'
于 2012-07-17T16:07:54.363 回答