mysql连接-非唯一表/别名错误

stszievb  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(294)

我正试图连接两个表,但出现了一个错误
'非唯一表/别名:'ct'
查询是

select em.* ,ct.Cities_NAME
from Customer em,
     Cities ct inner join Cities ct on ct.Cities_ID = em.Customer_CITY 
where Customer_GROUP is NULL and Customer_ENABLED is not FALSE and Customer_TYPE != 'User'

哪里出错了?为什么?

krugob8w

krugob8w1#

select em.* ,ct.Cities_NAME
 from Customer em , 
 Cities ct
 Where
 ct.Cities_ID = em.Customer_CITY -- it allows only matched id with city 
 and 
 Customer_GROUP is NULL  -- it accept when customer_group is null
 and 
 Customer_ENABLED not like '%FALSE%'  --it filter if customer enables is true
 and 
 Customer_TYPE not like '%User%'  -- it accepts customer type is not as user
z6psavjg

z6psavjg2#

不要在句子中使用逗号 FROM 条款。始终使用适当的、明确的、标准的 JOIN 语法:

select em.*, ct.Cities_NAME
from Customer em inner join
     Cities ct
     on ct.Cities_ID = em.Customer_CITY 
where em.Customer_GROUP is NULL and
      em.Customer_ENABLED and
      em.Customer_TYPE <> 'User';

出于某种原因,您列出了 ct 在查询中两次。您还应该限定查询中的所有列引用。

相关问题