列表

详情


183. 从不订购的客户

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
# Write your MySQL query statement below

mysql 解法, 执行用时: 539 ms, 内存消耗: 0 B, 提交时间: 2022-05-29 13:48:02

# Write your MySQL query statement below
select Name as Customers from Customers where Id not in (select CustomerId from Orders);

pythondata 解法, 执行用时: 308 ms, 内存消耗: 60 MB, 提交时间: 2023-08-07 16:32:20

import pandas as pd

def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
    # 选择 orders['customerId'] 中 'id' 不存在的行。
    df = customers[~customers['id'].isin(orders['customerId'])]

    # 创建一个只包含 name 列的数据框架
	# 并将列 name 重命名为 Customers。
    df = df[['name']].rename(columns={'name': 'Customers'})
    return df

mysql 解法, 执行用时: 546 ms, 内存消耗: 0 B, 提交时间: 2022-08-01 11:20:41

# Write your MySQL query statement below
select Name as Customers from Customers where id not in (select distinct CustomerId from Orders);

上一题