我有桌子,我想加入。它们与客户匹配号和客户匹配号相关联。我想从一个表中输入数据,然后与另一个表连接,然后显示第二个表中的结果。 我的代码如下所示:
SELECT [title],
[customer_no],
[forename],
[surname],
[res_addr_1],
[res_addr_2],
[city],
[country],
[res_postcode],
[DOB],
[Home_phone_no],
[Mobile_phone_no]
FROM Customer
LEFT JOIN Account ON cust_match_no
FROM Customer = customer_match_no
FROM Account
WHERE Account.account_no = '12345678';
但是我在“=”附近的语法不正确时出现错误,我不明白为什么!
答案 0 :(得分:1)
这不是FROM子句的目的。 FROM指定查询中的“主”表。
为了指定JOIN中哪些表应该匹配哪些字段,如果存在歧义,则需要使用表的名称或别名来限定这些字段,就像使用WHERE子句一样:WHERE Account.account_no = ...
。在您的情况下,似乎没有必要,因为来自Account的字段名为cust_match_no而另一个字段是customer_match_no,但无论如何您可以像这样重写您的查询:
SELECT
[title],
[customer_no],
[forename],
[surname],
[res_addr_1],
[res_addr_2],
[city],
[country],
[res_postcode],
[DOB],
[Home_phone_no],
[Mobile_phone_no]
FROM Customer cus
LEFT JOIN Account acc ON cus.cust_match_no = acc.customer_match_no
WHERE acc.account_no = '12345678';
如果SELECT子句中的列名不明确,则适用相同的建议。假设客户和账户表中都有一个“国家/地区”字段。然后,您应该对这些列进行限定以消除歧义:
SELECT
[Customer].[Country],
[Account].[Country]
....
PS。请原谅我的英文
答案 1 :(得分:0)
SELECT [title], [customer_no], [forename], [surname],
[res_addr_1], [res_addr_2], [city], [country], [res_postcode],
[DOB], [Home_phone_no], [Mobile_phone_no] FROM Customer
LEFT JOIN Account ON
cust_match_no = customer_no WHERE Account.account_no = '12345678';
答案 2 :(得分:0)
尝试这样:
SELECT
[title],
[customer_no],
[forename],
[surname],
[res_addr_1],
[res_addr_2],
[city],
[country],
[res_postcode],
[DOB],
[Home_phone_no],
[Mobile_phone_no]
FROM
Customer
LEFT JOIN
Account
ON
cust_match_no = customer_match_no
WHERE
Account.account_no = '12345678'
答案 3 :(得分:0)
SELECT [title], [customer_no], [forename], [surname], [res_addr_1], [res_addr_2],
[city], [country], [res_postcode], [DOB], [Home_phone_no], [Mobile_phone_no] FROM Customer
LEFT JOIN Account ON Account.cust_match_no = Customer.customer_match_no
WHERE Account.account_no = '12345678';
答案 4 :(得分:0)
试试这样:
SELECT [title],
[customer_no],
[forename],
[surname],
[res_addr_1],
[res_addr_2],
[city],
[country],
[res_postcode],
[DOB],
[Home_phone_no],
[Mobile_phone_no]
FROM Customer
LEFT JOIN Account ON cust_match_no = customer_match_no
WHERE Account.account_no = '12345678';