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 
      FROM Customer = customer_match_no 
      FROM Account  
 WHERE Account.account_no = '12345678';

但是,我在“=”附近不断收到错误语法错误,我不知道为什么!

4

5 回答 5

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。请原谅我的英语

于 2013-10-17T14:29:02.600 回答
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';  
于 2013-10-17T14:14:02.053 回答
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';
于 2013-10-17T14:01:56.043 回答
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'
于 2013-10-17T14:02:12.263 回答
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';
于 2013-10-17T14:10:01.933 回答