我需要执行一个查询,根据Incidents
从表CustomerID
获取所有内容。 CustomerID
来自MasterAccounts
表,该表与Incidents
表没有直接关系。有一个SubAccounts
表可以INNER JOIN
- 用来关联这两个表。
这是我到目前为止所做的:
SELECT
*
FROM
MasterAccounts AS ma
INNER JOIN
SubAccounts AS sa
INNER JOIN
Incidents AS i
WHERE
ma.CustomerID = sa.CustomerID;
AND sa.AccountID = i.AccountID
AND IncidentTypeID = 11;
失败了
关键字' WHERE'附近的语法不正确。
任何想法都将不胜感激!
答案 0 :(得分:5)
您需要在ON
之后使用INNER JOIN
定义加入条件,以链接这两个表:
SELECT
*
FROM
MasterAccounts AS ma
INNER JOIN
SubAccounts AS sa ON ma.CustomerID = sa.CustomerID;
INNER JOIN
Incidents AS i ON sa.AccountID = i.AccountID
WHERE
IncidentTypeID = 11;
这些列必须存在于所涉及的表中,并且应具有相同的数据类型(因为否则JOIN可能会导致"隐藏"数据类型转换,这在性能方面可能代价高昂)
答案 1 :(得分:3)
大多数WHERE条件应该在每个连接的ON子句中:
SELECT i.*
FROM MasterAccounts ma
INNER JOIN SubAccounts sa ON sa.CustomerID = ma.CustomerID
INNER JOIN Incidents i ON i.AccountID = sa.AccountID
WHERE IncidentTypeID = 11;
答案 2 :(得分:2)
正确的语法是:
SELECT
*
FROM
MasterAccounts AS ma
INNER JOIN
SubAccounts AS sa ON ma.CustomerID = sa.CustomerID;
INNER JOIN
Incidents AS i ON sa.AccountID = i.AccountID
WHERE
IncidentTypeID = 11;
希望这有帮助!