2

尝试在查询中获取特定值时收到以下消息

“指定的字段可以引用多个表”

很明显,我正在尝试搜索多个表中存在的内容,但是如何正确执行此操作?

现在我有以下代码:

SELECT Table1.CustomerId, Table1.Address, 
       Table2.CustomerId, Table2.Telephone, 
       Table3.CustomerId, Table3.Notes
FROM (Table1 INNER JOIN Table2 ON Table1.CustomerId=TAble2.CustomerId) 
      INNER JOIN Table3 ON Table2.CustomerId=Table3.CustomerId
WHERE CustomerId = 0015

最后一句话是问题......有什么想法吗?

4

3 回答 3

6

错误消息非常清楚,子句CustomerId中的字段同时出现在两个表中。您必须确定要从哪个表中使用它;还是?例如:WHEREWHERE CustomerId = 0015table1table2

SELECT Table1.CustomerId, Table1.Address, 
       Table2.CustomerId, Table2.Telephone, 
       Table3.CustomerId, Table3.Notes
FROM (Table1 INNER JOIN Table2 ON Table1.CustomerId=TAble2.CustomerId) 
      INNER JOIN Table3 ON Table2.CustomerId=Table3.CustomerId
WHERE table1.CustomerId = 0015
于 2013-08-21T12:51:54.040 回答
5

CustomerId您应该在 WHERE 语句中指出哪个表来自

SELECT Table1.CustomerId, Table1.Address, 
       Table2.CustomerId, Table2.Telephone, 
       Table3.CustomerId, Table3.Notes
FROM (Table1 INNER JOIN Table2 ON Table1.CustomerId=TAble2.CustomerId) 
      INNER JOIN Table3 ON Table2.CustomerId=Table3.CustomerId
WHERE Table1.CustomerId = 0015
于 2013-08-21T12:52:38.630 回答
1

您需要从需要 customerId 的表中初始化。

SELECT Table1.CustomerId, Table1.Address, 
       Table2.CustomerId, Table2.Telephone, 
       Table3.CustomerId, Table3.Notes
FROM (Table1 INNER JOIN Table2 ON Table1.CustomerId=TAble2.CustomerId) 
      INNER JOIN Table3 ON Table2.CustomerId=Table3.CustomerId
WHERE table1.CustomerId = 0015
于 2013-08-21T12:54:59.987 回答