首页 > 数据库技术 > 详细

[LeetCode]1084. 销售分析III(Mysql,having+聚合函数)

时间:2020-06-13 01:03:07      阅读:51      评论:0      收藏:0      [点我收藏+]

题目

Table:?Product

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
| unit_price   | int     |
+--------------+---------+
product_id 是这个表的主键
Table:?Sales

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| seller_id   | int     |
| product_id  | int     |
| buyer_id    | int     |
| sale_date   | date    |
| quantity    | int     |
| price       | int     |
+------ ------+---------+
这个表没有主键,它可以有重复的行.
product_id 是 Product 表的外键.
?

编写一个SQL查询,报告2019年春季才售出的产品。即仅在2019-01-01至2019-03-31(含)之间出售的商品。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sales-analysis-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

  • 使用having+聚合函数过滤 来代替子查询

代码

# Write your MySQL query statement below
select p.product_id,product_name
from Product p join Sales s
on p.product_id=s.product_id
group by p.product_id
having min(sale_date)>= ‘2019-01-01‘ and max(sale_date)<=‘2019-03-31‘

[LeetCode]1084. 销售分析III(Mysql,having+聚合函数)

原文:https://www.cnblogs.com/coding-gaga/p/13111072.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!