MySQL-比较两个表中的客户ID,以确定谁没有购物

时间:2018-11-14 03:50:51

标签: mysql sql select distinct

我在SQL中有两个表,一个表包含有关客户和他们所下的订单的信息(列包括customerid,contactname,orderid,数量,仅举几例)。我的第二张表只是所有客户ID的列表,我的任务是确定哪个客户ID没有购物。一些客户ID多次购买,因此我不确定如何使用SELECT DISTINCT比较两个表。

2 个答案:

答案 0 :(得分:1)

加入第二张表并过滤结果

    SELECT DISTINCT t1.customerid, t1.contactname
    FROM table1 t1
    JOIN table2 t2
    ON t1.customerid = t2.customerid
    WHERE t1.customerid = t2.customerid

答案 1 :(得分:0)

使用not exists

select t2.customerid
from table2 t2
where not exists (select 1 from table1 t1 where t1.customerid = t2.customerid);
相关问题