题干
Customers 表:
±------------±--------+
| Column Name | Type |
±------------±--------+
| id | int |
| name | varchar |
±------------±--------+
在 SQL 中,id 是该表的主键。
该表的每一行都表示客户的 ID 和名称。
Orders 表:
±------------±-----+
| Column Name | Type |
±------------±-----+
| id | int |
| customerId | int |
±------------±-----+
在 SQL 中,id 是该表的主键。
customerId 是 Customers 表中 ID 的外键( Pandas 中的连接键)。
该表的每一行都表示订单的 ID 和订购该订单的客户的 ID。
找出所有从不点任何东西的顾客。以 任意顺序 返回结果表,结果格式如下所示。
示例
解答
主表是customers表,有过购买记录的会出现在orders表,因此只需将两表进行左连接,筛选出orderid为null的顾客,即为从未购买过的顾客
with t1 as(
select cus.id,cus.name as customers,od.customerid,od.id as orderid
from customers as cus left join orders as od
on od.customerid=cus.id
)
select customers from t1 where orderid is null