gpt4 book ai didi

mysql - SQL初学者练习题

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:23 26 4
gpt4 key购买 nike

给定两个表,orders (order_id, date, $, customer_id)customers (ID, name)

这是我的方法,但我不确定它是否有效&我想知道是否有更快/更好的方法来解决这些问题:

1) 找出在 date 7/9/2018

至少下了一个 ordercustomers 的数量
Select count (distinct customer_id)
From
(
Select customer_id from orders a
Left join customer b
On a.customer_id = b.ID
Group by customer_id,date
Having date = 7/9/2018
) a

2) 找出在 7/9/2018 没有下订单客户的数量

Select count (customer_id) from customer where customer_id not in
(
Select customer_id from orders a
Left join customer b
On a.customer_id = b.ID
Group by customer_id,date
Having date = 7/9/2018
)

3) 在 7 月 1 日和 7 月 30 日之间找到销售额最高的日期

select date, max($)
from (
Select sum($),date from orders a
Left join customer b
On a.customer_id = b.ID
Group by date
Having date between 7/1 and 7/30
)

谢谢,

最佳答案

对于问题 1,有效的解决方案可能如下所示:

SELECT COUNT(DISTINCT customer_id) x
FROM orders
WHERE date = '2018-09-07'; -- or is that '2018-07-09' ??

对于问题 2,有效的解决方案可能如下所示:

SELECT COUNT(*) x
FROM customer c
LEFT
JOIN orders o
ON o.customer_id = x.customer_id
AND o.date = '2018-07-09'
WHERE o.crder_id IS NULL;

假设没有关系,问题 3 的有效解决方案可能如下所示:

SELECT date 
, COUNT(*) sales
FROM orders
WHERE date BETWEEN '2018-07-01' AND '2018-07-30'
GROUP
BY date
ORDER
BY sales DESC
LIMIT 1;

关于mysql - SQL初学者练习题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51270730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com