sql - MS ACCESS 2016 多重联接语法

标签 sql ms-access

如何使用 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 your JOIN operation is incomplete or contains too many tables. You may want to put your ON expression in a WHERE 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/

相关文章:

MySQL 最大平均值按 2 个字段分组

sql - 使用 ADODB 连接从关闭的工作簿中检索数据。某些数据被跳过?

sql - 使用动态模式名​​称运行多个 Insert 语句

sql-server-2008 - 通过 VBA 从传递查询返回值

C# SQL SUM 值到标签

php - mysql 根据 LEAST 值返回正确的链接

sql - 分析 SQL 查询

ms-access - 如何在运行时在 VBA 代码中更改 MS Access 子窗体的 View ?

ms-access - VBA:词频数组

java.lang.ClassNotFoundException : sun. jdbc.odbc.JdbcOdbcDriver 发生异常。为什么?