行业知识
LeetCode #1327 列出指定时间段内所有的下单产品
2026-10-19 17:31:17
来源:互联网转载
题目
表: Products
()+------------------+---------+| Column Name | Type |+------------------+---------+| product_id | int || product_name | varchar || product_category | varchar |+------------------+---------+product_id 是该表主键(具有唯一值的列)。该表包含该公司产品的数据。
表: Orders
+---------------+---------+| Column Name | Type |+---------------+---------+| product_id | int || order_date | date || unit | int |+---------------+---------+该表可能包含重复行。product_id 是表单 Products 的外键(reference 列)。unit 是在日期 order_date 内下单产品的数目。
写一个解决方案,要求获取在 2020 年 2 月份下单的数量不少于 100 的产品的名字和数目。
()返回结果表单的 顺序无要求 。
查询结果的格式如下。
示例 1:
输入:Products 表:+-------------+-----------------------+------------------+| product_id | product_name | product_category |+-------------+-----------------------+------------------+| 1 | Leetcode Solutions | Book || 2 | Jewels of Stringology | Book || 3 | HP | Laptop || 4 | Lenovo | Laptop || 5 | Leetcode Kit | T-shirt |+-------------+-----------------------+------------------+Orders 表:+--------------+--------------+----------+| product_id | order_date | unit |+--------------+--------------+----------+| 1 | 2020-02-05 | 60 || 1 | 2020-02-10 | 70 || 2 | 2020-01-18 | 30 || 2 | 2020-02-11 | 80 || 3 | 2020-02-17 | 2 || 3 | 2020-02-24 | 3 || 4 | 2020-03-01 | 20 || 4 | 2020-03-04 | 30 || 4 | 2020-03-04 | 60 || 5 | 2020-02-25 | 50 || 5 | 2020-02-27 | 50 || 5 | 2020-03-01 | 50 |+--------------+--------------+----------+输出:+--------------------+---------+| product_name | unit |+--------------------+---------+| Leetcode Solutions | 130 || Leetcode Kit | 100 |+--------------------+---------+解释:2020 年 2 月份下单 product_id = 1 的产品的数目总和为 (60 + 70) = 130 。2020 年 2 月份下单 product_id = 2 的产品的数目总和为 80 。2020 年 2 月份下单 product_id = 3 的产品的数目总和为 (2 + 3) = 5 。2020 年 2 月份 product_id = 4 的产品并没有下单。2020 年 2 月份下单 product_id = 5 的产品的数目总和为 (50 + 50) = 100 。
分析
一个产品表,一个订单表表,查询指定日期内大于指定数量的产品信息。首先关联两表,有两个条件,一指定日期,二指定数量。指定日期可用between...and,之后要统计产品订单信息,就要对产品进行分组统计group by求和sum(),然后对分组统计后的结果进行过滤having
指定日期的多种方法
- order_date between '2020-02-01' and '2020-02-29'
- order_date like '2020-02%'
- DATE_FORMAT(order_date, "%Y-%m") = "2020-02"
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。
- LEFT(order_date, 7) 或 substr(1, 7)
直接截取日期的前7位"2020-02"
题解
select product_name, sum(unit) unit from products p, orders o where p.product_id = o.product_id and o.order_date like '2020-02%' group by p.product_id having unit >= 100
输入
Products
| product_id | product_name | product_category || ---------- | --------------------- | ---------------- || 1 | Leetcode Solutions | Book || 2 | Jewels of Stringology | Book || 3 | HP | Laptop || 4 | Lenovo | Laptop || 5 | Leetcode Kit | T-shirt |
Orders
| product_id | order_date | unit || ---------- | ---------- | ---- || 1 | 2020-02-05 | 60 || 1 | 2020-02-10 | 70 || 2 | 2020-01-18 | 30 || 2 | 2020-02-11 | 80 || 3 | 2020-02-17 | 2 || 3 | 2020-02-24 | 3 || 4 | 2020-03-01 | 20 || 4 | 2020-03-04 | 30 || 4 | 2020-03-04 | 60 || 5 | 2020-02-25 | 50 || 5 | 2020-02-27 | 50 || 5 | 2020-03-01 | 50 |
预期结果
| product_name | unit || ------------------ | ---- || Leetcode Solutions | 130 || Leetcode Kit | 100 |
下一篇:战地1怎么找空的服务器?