
sql 实现思路
如何查询在指定时间段内连续多日都有特定商品库存的商店?以下是一个 sql 实现思路:
通过 union 操作符,把每天库存信息拼接到一起,形成一张临时表:
select 商店 from 表 where 日期='2021-09-01' -- and 后续可以添加不同的商品信息 union select 商店 from 表 where 日期='2021-09-02' union select 商店 from 表 where 日期='2021-09-03'
然后对临时表进行分组,统计每家商店出现在不同日期的次数:
select 商店,count(distinct 日期) countdate
from (
select 商店 from 表 where 日期='2021-09-01' -- and 后续可以添加不同的商品信息
union
select 商店 from 表 where 日期='2021-09-02'
union
select 商店 from 表 where 日期='2021-09-03'
) as tab
group by 商店最后,筛选出连续多日都有库存的商店:
HAVING countdate=3
这样,就可以查询到在 9 月 1 日、2 日、3 日都有指定商品库存的商店。
以上就是如何用 SQL 查询在指定时间段内连续多日都有特定商品库存的商店?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号