1

我无法将 3 张桌子连接在一起。我有一个SELECT适用于其中两个表格的表格,这是我想查看的主要数据。我还想将税率和税金添加到所选单位。它们存储在不同的表中,因为在我的情况下,单位可以有多个速率。这是有效的查询:

SELECT u.unit_name from units u 
LEFT JOIN reservations r ON r.unit = u.id
    AND r.arrival <= 2013-10-11
    AND r.departure >= 2013-10-01
WHERE r.unit IS NULL

而我试图做的不是:

SELECT u.unit_name, ur.unit, ur.rate, ur.tax
FROM units u
LEFT JOIN reservations r 
INNER JOIN unit_rates ur 
    ON r.unit = u.id
    ON ur.unit = u.id
    AND r.arrival <= 2013-10-11
    AND r.departure >= 2013-10-01
WHERE r.unit IS NULL 
    AND ur.active = TRUE

这里是 SQLfiddle:http ://sqlfiddle.com/#!2/b303ad

4

2 回答 2

2
SELECT u.unit_name, ur.unit, ur.rate, ur.tax 
FROM units u 
LEFT JOIN reservations r ON r.unit = u.id
AND r.arrival <= 2013-10-11
AND r.departure >= 2013-10-01
JOIN unit_rates ur ON ur.unit = u.id
WHERE r.unit IS NULL

在每个 JOIN 之后放置 ON 条件。

SQL小提琴

于 2013-10-30T18:04:13.213 回答
0

只需AND在您的 ON 中使用:

SELECT u.unit_name, ur.unit, ur.rate, ur.tax
FROM units u
LEFT JOIN reservations r
INNER JOIN unit_rates ur 
/* Another table */
ON r.unit = u.id
    AND ur.unit = u.id
    AND r.arrival <= 2013-10-11
    AND r.departure >= 2013-10-01
WHERE r.unit IS NULL AND ur.active = TRUE
于 2013-10-30T18:07:12.423 回答