【SQL】1327. 列出指定时间段内所有的下单产品

前述

知识点:sql中用JOIN USING 简化JOIN ON

题目描述

leetcode 题目:1327. 列出指定时间段内所有的下单产品



Code

写法一

先过滤,再连表

sql 复制代码
-- 写法一:先过滤再连表
select B.product_name, A.summ as unit
from(
    select product_id,
        sum(unit) as summ
    from Orders
    where order_date between '2020-02-01' and '2020-02-29'
    group by product_id
    having summ >= 100
) A
left join Products B
on A.product_id = B.product_id;

写法二

先连表,再过滤

sql 复制代码
-- 写法二:先连表再过滤
select product_name, sum(unit) as unit
from Products P
left join Orders O
using(product_id)
-- where order_date between '2020-02-01' and '2020-02-29'
-- where order_date >= '2020-02-01' and order_date <='2020-02-29'
-- where year(order_date) = 2020 and month(order_date) = 02
where order_date like '2020-02%'
group by O.product_id
having sum(unit) >= 100;

不同的过滤条件写法,都行。

相关推荐
哆啦A梦15883 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
Zzzzmo_3 小时前
【MySQL】JDBC(含settings.xml文件配置/配置国内镜像以及pom.xml文件修改)
数据库·mysql
参.商.3 小时前
【Day41】143. 重排链表
leetcode·golang
FirstFrost --sy4 小时前
MySQL内置函数
数据库·mysql
2401_879693874 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
reembarkation4 小时前
光标在a-select,鼠标已经移出,下拉框跟随页面滚动
java·数据库·sql
eggwyw4 小时前
MySQL-练习-数据汇总-CASE WHEN
数据库·mysql
星轨zb4 小时前
通过实际demo掌握SpringSecurity+MP中的基本框架搭建
数据库·spring boot·spring security·mp
treacle田5 小时前
达梦数据库-配置本地守护进程dmwatcher服务-记录总结
数据库·达梦数据库·达梦数据库local数据守护
wyt5314295 小时前
Redis的安装教程(Windows+Linux)【超详细】
linux·数据库·redis