如何使用 MS ACCESS 2016 语法编写以下 SQL 查询?
SELECT
ticket.ticket_id,
a1.attr_val AS attr_val1,
a2.attr_val AS attr_val2,
a3.attr_val AS attr_val3
FROM ticket
LEFT JOIN attr a1 ON ticket.ticket_id=a1.ticket_id AND a1.attr_type=1
LEFT JOIN attr a2 ON ticket.ticket_id=a2.ticket_id AND a2.attr_type=2
LEFT JOIN attr a3 ON ticket.ticket_id=a3.ticket_id AND a3.attr_type=3
预先感谢您的帮助!
最佳答案
MS Access 有这些 weird requirements about parentheses :
Join expression not supported. (Error 3296)
Possible causes:
- Your SQL statement contains multiple joins in which the results of the query can differ, depending on the order in which the joins are performed. You may want to create a separate query to perform the first join, and then include that query in your SQL statement.
- The
ON
statement in yourJOIN
operation is incomplete or contains too many tables. You may want to put yourON
expression in aWHERE
clause.
上述引用中建议的解决方案并不总是合适(例如,使用 WHERE
子句不是 LEFT JOIN
条件的选项)。
如何解决:
- 将连接放在括号中,这样每个连接表达式中只有一个连接;
- 用括号将
ON
表达式括起来;
SQL:
SELECT
ticket.ticket_id,
a1.attr_val AS attr_val1,
a2.attr_val AS attr_val2,
a3.attr_val AS attr_val3
FROM ((ticket
LEFT JOIN attr AS a1 ON (ticket.ticket_id=a1.ticket_id AND a1.attr_type=1))
LEFT JOIN attr AS a2 ON (ticket.ticket_id=a2.ticket_id AND a2.attr_type=2))
LEFT JOIN attr AS a3 ON (ticket.ticket_id=a3.ticket_id AND a3.attr_type=3)
关于sql - MS ACCESS 2016 多重联接语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37399840/