MySQL Join-非唯一表/别名错误

时间:2018-09-17 10:55:00

标签: mysql sql join inner-join unique

我正在尝试加入2个表,但出现错误

  

'非唯一表/别名:'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'

错误在哪里,为什么?

2 个答案:

答案 0 :(得分:1)

从不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。您还应该限定查询中的所有列引用。

答案 1 :(得分:0)

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