【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;

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

相关推荐
jiayou642 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤2 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区3 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1774 天前
《从零搭建NestJS项目》
数据库·typescript
加号34 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏4 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐4 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
琢磨先生David4 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
百锦再4 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
tryCbest4 天前
数据库SQL学习
数据库·sql