584. 寻找用户推荐人

题目

给定表 customer ,里面保存了所有客户信息和他们的推荐人。

1
2
3
4
5
6
7
8
9
10
11
+------+------+-----------+
| id | name | referee_id|
+------+------+-----------+
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
+------+------+-----------+

写一个查询语句,返回一个编号列表,列表中编号的推荐人的编号都 不是 2。

对于上面的示例数据,结果为:

1
2
3
4
5
6
7
8
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

解法

解法一:

直接查询referee_id不为2并且为null的

SQL

1
select name from customer where referee_id != 2 or referee_id is null

解法二:

官方题解

SQL

1
2
3
4
5
6
select 
round(
(select count(*) from Delivery where order_date=customer_pref_delivery_date) /
(select count(*) from Delivery), 4
) * 100
as immediate_percentage

解法三:

条件选择

SQL

1
2
3
4
5
6
select round(
sum(case when order_date=customer_pref_delivery_date then 1 else 0 end) /
count(*)
,4)*100
as immediate_percentage
from Delivery

解法四:

逻辑真值

1
2
3
4
5
6
select round(
sum(order_date=customer_pref_delivery_date) /
count(*)
,4)*100
as immediate_percentage
from Delivery

参考资料

LeetCode-1173. 即时食物配送 I(简单)

0%