0

我想从三个 3 表中的 mysql 获取数据。我正在使用LEFT OUTER JOIN. 这是我的查询:

SELECT cms_addresses.address1 as add1, 
  cms_addresses.email as ademail, 
  cms_addresses.city as adcity, 
  cms_addresses.clientid 
  cms_addresses.country as adcountry, 
  cms_addresses.province as adprovince, 
  cms_addresses.postal as adprovince, 
  cms_delivery_info.d_address1, 
  cms_delivery_info.d_city, 
  cms_delivery_info.d_country, 
  cms_delivery_info.d_province, 
  cms_delivery_info.d_postal, 
  cms_orders.id, 
  cms_orders.cdate, 
  cms_orders.message 
FROM cms_addresses 
LEFT OUTER JOIN cms_delivery_info 
  ON cms_addresses.clientid = cms_delivery_info.d_clientid 
LEFT OUTER JOIN cms_orders = cms_addresses.clientid = cms_orders.clientid
WHERE cms_orders.id = 10
  and cms_addresses.addresstypeid = 1

问题是这个查询没有运行并且给了我那个错误。

不是唯一的表/别名:'cms_addresses'

请帮我解决这个问题。谢谢

4

3 回答 3

0

您在第二个左连接中有一个语法错误,表名后面有'=' 试试这个::

SELECT 
cms_addresses.address1 as add1, 
cms_addresses.email as ademail, 
cms_addresses.city as adcity, 
cms_addresses.clientid, 
cms_addresses.country as adcountry, 
cms_addresses.province as adprovince, 
cms_addresses.postal as adprovince, 
cms_delivery_info.d_address1, 
cms_delivery_info.d_city, 
cms_delivery_info.d_country, 
cms_delivery_info.d_province, 
cms_delivery_info.d_postal, 
cms_orders.id, 
cms_orders.cdate, 
cms_orders.message 

FROM cms_addresses 

LEFT OUTER JOIN cms_delivery_info ON cms_addresses.clientid = cms_delivery_info.d_clientid 
LEFT OUTER JOIN cms_orders on cms_addresses.clientid = cms_orders.clientid 
WHERE cms_orders.id = 10 and cms_addresses.addresstypeid = 1
于 2013-01-06T06:26:05.267 回答
0

cms_addresses.clientid在和之间加一个逗号cms_addresses.country AS adcountry,看看它是否有效

SELECT  cms_addresses.address1 AS add1,
        cms_addresses.email AS ademail,
        cms_addresses.city AS adcity,
        cms_addresses.clientid,                  // add comma here
        cms_addresses.country AS adcountry,
        cms_addresses.province AS adprovince,
        cms_addresses.postal AS adprovince,
        cms_delivery_info.d_address1,
        cms_delivery_info.d_city,
        cms_delivery_info.d_country,
        cms_delivery_info.d_province,
        cms_delivery_info.d_postal,
        cms_orders.id,
        cms_orders.cdate,
        cms_orders.message
FROM    cms_addresses
        LEFT JOIN cms_delivery_info
                ON cms_addresses.clientid = cms_delivery_info.d_clientid
        LEFT JOIN cms_orders 
                // use ON instead of = (equal sign) here
                ON cms_addresses.clientid = cms_orders.clientid
WHERE   cms_orders.id = 10 AND cms_addresses.addresstypeid = 1
于 2013-01-06T06:26:38.400 回答
0

在你SELECT的某个时刻,你有:

cms_addresses.clientid cms_addresses.country as adcountry,

看起来你错过了,某个地方?

在你的FROM你有:

LEFT OUTER JOIN cms_orders = cms_addresses.clientid = cms_orders.clientid 

我再次猜测这是一个错字(首先=应该是 a ON)。

您收到的消息意味着您正在重用链中的表cms_addressesJOIN并且您没有给它一个别名,这是消歧所必需的,但在您的情况下,这很奇怪,因为您在子句中没有cms_addresses超过一次。FROM

我会尝试重写为所有表提供别名的查询。还启用完整日志记录并检查真正到达 Mysql 的内容。

于 2013-01-06T06:40:49.177 回答