当前位置:网站首页>MySQL使用聚合函数可以不搭配GROUP BY分组吗?
MySQL使用聚合函数可以不搭配GROUP BY分组吗?
2022-08-05 09:28:00 【CSDN问答】
本人是入门没多久的数据产品,今天写了一条查询最近15天没产生过订单的store。数据开发写的SQL比我写的出结果速度快了8倍。
有个地方实在没看懂,虚心请教。
我写的
SELECT s.id FROM `store`s JOIN ( SELECT `store_id` , MAX( `create_time`)t FROM `order` GROUP BY `store_id` )aON s.id = a.store_idWHERE s.channel = 2 AND s.show = 1 AND DATEDIFF( now(), a.t ) > 15数据开发写的
SELECT idFROM `store`s where s.channel = 2 AND s.show = 1 AND DATEDIFF(now(), (SELECT MAX( `create_time`) FROM `order` o where o.store_id = s.id)) > 15看了下我SQL的运行性能分析,是因为order表数据太多行(大概有几千万吧),用了GROPU BY分组导致性能。
开发写的避开了使用GROPU BY ,但本人功力较浅,表示看不懂了:
在AND DATEDIFF(now(), (SELECT MAX( `create_time`) FROM `order` o where o.store_id = s.id)) > 15的这个查询条件中,子查询语句里面输出MAX的时间,但是没有使用分组聚合的话,输出的不应该是只有一行数据吗?为什么能实现我按照store_id进行分组的需求?虽然大概猜到核心在于后面的o.store_id = s.id,但表示没看懂
边栏推荐
- Why do I recommend using smart async?
- 新白娘子传奇系列
- Is there a problem with writing this?How to synchronize data in sql-client
- MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
- C语言-数组
- Does flink cdc support synchronization from oracle dg library?
- 欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
- 【LeetCode】623. Add a row to the binary tree
- 16 kinds of fragrant rice recipes
- Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood
猜你喜欢
随机推荐
Example of Noise Calculation for Amplifier OPA855
XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
sphinx matches the specified field
512-color chromatogram
16种香饭做法全攻略
长达四年的减肥记录
随时牵手 不要随意分手[转帖]
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
thinkPHP5 realizes clicks (data increment/decrement)
Code Audit - PHP
Undefined symbols for architecture arm64解决方案
如何实现按键的短按、长按检测?
代码审计—PHP
DPU — 功能特性 — 管理系统的硬件卸载
Redis源码解析:Redis Cluster
C语言-数组
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
深度学习21天——卷积神经网络(CNN):服装图像分类(第3天)









