选择没有地址的客户

时间:2015-06-23 08:36:14

标签: mysql

有两个表ps_customer和ps_address。

ps_customer contains: id_customer and customer.email
ps_address contains: id_customer and few other fields.

我想选择ps_address表中不存在数据的客户。 我正在使用Sql Query:

SELECT id_customer, customer.email FROM ps_customer 
WHERE id_customer NOT IN SELECT id_customer FROM ps_address;

但它会引发查询SELECT id_customer FROM ps_address

出错的错误

5 个答案:

答案 0 :(得分:1)

你需要在这两张桌子上进行LEFT OUTER JOIN。

SELECT PSC.id_customer, PSCcustomer.email
FROM ps_customer PSC
LEFT OUTER JOIN ps_address PSA on PSC.id_customer = PSA.id_customer 
WHERE PSA.id_customer IS NULL

答案 1 :(得分:0)

您需要在查询的最后部分放置括号,以表明它是子查询:

SELECT id_customer, customer.email FROM ps_customer 
WHERE id_customer NOT IN ( SELECT id_customer FROM ps_address );

答案 2 :(得分:0)

您可以使用左连接作为

select 
c.id_customer,
c.customer.email 
from ps_customer c
left join ps_address a on a.id_customer = c.id_customer
where a.id_customer is null

答案 3 :(得分:0)

您可以尝试使用左连接

SELECT id_customer, customer.email FROM ps_customer pc left join ps_address pa
on pc.id_customer = pa.id_customer
where pa.id_customer is null;

答案 4 :(得分:0)

您可以使用左连接:

SELECT id_customer, customer.email 
FROM ps_customer 
   left join ps_address on ps_customer.id_customer= ps_address.id_customer 
where ps_address.id_customer is null;

或者如果你想使用子查询,你必须在子查询周围使用()

SELECT id_customer, customer.email FROM ps_customer 
WHERE id_customer NOT IN ( SELECT id_customer FROM ps_address );