当前位置:网站首页>How can MySQL query the records with the largest consumption amount of each user?
How can MySQL query the records with the largest consumption amount of each user?
2022-06-22 07:56:00 【CSDN Q & A】
There was a problem during the interview Query the consumption amount of each user Table structure [id, user_id, amount]

I want to :
SELECT user_id, max( amount ) FROM orders GROUP BY user_id But I see that many statements given by people in the community use subqueries . Why is it so complicated ?
such as :
SELECT o.* FROM orders oRIGHT JOIN ( SELECT max(amount) mxa, user_id FROM orders GROUP BY user_id ) m ON o.user_id = m.user_id AND o.amount = m.mxaTake the answer :
Because the query fields are different . It's ok if you just check the maximum amount , But if you need to query other relevant information about the order , Your writing is not suitable .
Due to the use of group by grouping . The normal field of the query must be in the group field .
SELECT o.* FROM orders oRIGHT JOIN ( SELECT max(amount) mxa, user_id FROM orders GROUP BY user_id ) m ON o.user_id = m.user_id AND o.amount = m.mxa This method is aimed at real scenes , Query the order record information corresponding to each user's consumption amount .
The logic of implementation : The sub query finds the maximum order amount of each user , Then, it is related and matched according to the order amount , Find the corresponding order record .
边栏推荐
- setneedsdisplay layoutifneeded setNeedsLayout
- AutoCAD 2020.3 Chinese Version (old version)
- Usage and understanding of async/await in JS
- Maptalks: basic operation and wms/wmts map service loading
- 由浅入深拿下OpenFeign
- Canvastotempfilepath of wechat
- User defined pop-up use
- Semaphore
- Layer drawing method
- Model electricity experiment -- Experiment 1 transistor common emitter single transistor amplifier
猜你喜欢
![[songhongkang MySQL database] [advanced chapter] [07] MySQL storage engine](/img/47/62ad1e661e015438c40574f533d8f1.png)
[songhongkang MySQL database] [advanced chapter] [07] MySQL storage engine
![[普通物理]波的能量与干涉](/img/fe/066aa9e8ed776b8f069b59b7123367.png)
[普通物理]波的能量与干涉

An example shows the difference between let and VaR

模电实验——实验二 JFET共源极放大电路

丰田bZ4X取消上市发布会,就算低温充电问题不存在,产品力如何?

Applet /vant UI to upload files

Model electricity experiment -- Experiment 1 transistor common emitter single transistor amplifier

Toyota bz4x canceled the launch conference. Even if the low-temperature charging problem does not exist, how about the product strength?

【Oracle 数据库】奶妈式教程 day13 日期函数

XMIND 2022 mind map active resources?
随机推荐
OSI and tcp/ip
LR 2022 ultra detailed installation tutorial "latest"
MTK platform execution disable_ Interrupt signals masked during IRQ will be enabled when I execute enable_ Continue triggering after IRQ
5、 Image component
《百度搜索引擎网页质量白皮书》指导网站建设及优化
Realization of readable and writable files in library management system with C language bidirectional linked list
6、 Scrollview component
(7)双向链表
Qualcomm platform msm8953 display subsystem learning
A training summary of Intranet penetration test
Scrollrect for tableview
Energy and interference of waves
Model electricity experiment -- Experiment 2 JFET common source amplifier circuit
[common template problems in graph theory] four shortest path solutions and two minimum spanning tree solutions
Simplicity is the best method of network promotion
Cocoapods problem record
easy-rule 初窥
(9)顺序队列与栈队列
Charles uses
Technology blog collection