【问题标题】:syntax error when trying to join in Access尝试加入 Access 时出现语法错误
【发布时间】:2013-11-15 03:31:58
【问题描述】:

我的数据库包含几个查找表(在 UI 表单上显示为下拉菜单)。

例如,

customer_data - 客户人口统计信息。

lookup_car - 存储汽车描述(Pinto、Vega、Reliant Robin、Mustang、Corvette)

junction_car_customer - 加入拥有一辆或多辆汽车的客户

客户 Jeremy Clarkson (cust_id: 1) 拥有三辆汽车。他的记录下拉菜单显示:

Pinto (car_id=100)
Reliant Robin (car_id=101)
Vega (car_id=102)

junction_car_customer 数据如下所示:

cust_id    car_id
1          100
1          101
1          102

我正在尝试返回显示客户名称和拥有的模型的行。

这是我的查询:

SELECT 
 cd.cust_id,
 cd.name_first,
 cd.name_last,
 jcc.car_id,
 lc.car_desc
FROM
 customer_data AS cd
 LEFT JOIN ju_cust_car AS jcc ON jcc.cust_id = cd.cust_id
 LEFT JOIN lookup_cars AS lc ON lc.car_id = jcc.car_id

ORDER BY 
 cd.name_last

我得到:

查询表达式 'jcc.cust_id = 中的语法错误(缺少运算符) cd.cust_id LEFT JOIN lookup_cars AS lc ON lc.car_id = jcc.car_id'

是什么导致了这个错误?

【问题讨论】:

  • @Deepshikha:上面的查询可能看起来不错,但在 Access 中不起作用。

标签: sql ms-access select join syntax-error


【解决方案1】:

访问需要括号来表示多个连接。例如:

select * 
from ((Table1 as t1)
left join Table2 as t2 on t1.id = t2.id)
left join Table3 as t3 on t1.id = t3.id

【讨论】:

  • 所以我只需要加入第一个查询,或者这些括号会与更多加入的表级联吗?
  • 它们级联到更多连接。对于三个连接,您需要三对括号。
【解决方案2】:

访问对于 LEFT/RIGHT JOIN 和括号有点挑剔。试试这个

SELECT 
    cd.cust_id,
    cd.name_first,
    cd.name_last,
    jcc.car_id,
    lc.car_desc
FROM
    (
        customer_data AS cd
        LEFT JOIN 
        ju_cust_car AS jcc 
            ON jcc.cust_id = cd.cust_id
    )
    LEFT JOIN lookup_cars AS lc 
        ON lc.car_id = jcc.car_id
ORDER BY 
 cd.name_last

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 2021-05-19
    • 2017-12-11
    • 2016-08-09
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多