How to use MySQL group by and order by together

How to use MySQL group by and order by together

Suppose there is a table: reward (reward table), the table structure is as follows:

CREATE TABLE test.reward (
 id int(11) NOT NULL AUTO_INCREMENT,
 uid int(11) NOT NULL COMMENT 'user uid',
 money decimal(10, 2) NOT NULL COMMENT 'Reward amount',
 datatime datetime NOT NULL COMMENT 'time',
 PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci
COMMENT = 'Reward table';

The data in the table are as follows:

Now we need to query the highest reward received by each person and sort them from largest to smallest:

If you query directly:

SELECT id, uid, money, datatime FROM reward GROUP BY uid ORDER BY money DESC;

The following results are obtained:

We did not get the results we needed. This is because when group by and order by are used together, group by is used first, and the first piece of data after grouping is taken out. Therefore, the subsequent order by sorting is based on the first piece of data taken out, but the first piece of data is not necessarily the largest piece of data in the group.

Method 1:

In this case we can sort first, then group, and use a subquery.

SELECT
 r.id,
 r.uid,
 r.money,
 r.datatime
FROM (SELECT
  id,
  uid,
  money,
  datatime
 FROM reward
 ORDER BY money DESC) r
GROUP BY r.uid
ORDER BY r.money DESC;

Method 2:

If you don't need to get the entire record, you can use max() min()

SELECT id, uid, money, datatime, MAX(money) FROM reward GROUP BY uid ORDER BY MAX(money) DESC;

The result is:

You may have discovered that the money field and max(money) field of the records obtained using max() are inconsistent. This is because only the maximum value of the uid is retrieved here, but the entire record corresponding to the maximum value is not retrieved.

If you need to get the entire record, you cannot use this method and can use a subquery.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the group by statement in MySQL database group query
  • Detailed explanation of group by and having in MySQL
  • MySQL efficient query left join and group by (plus index)
  • Detailed explanation of MySQL Group by optimization
  • How to optimize MySQL group by statement
  • mysql group by grouping multiple fields
  • Implement group by based on MySQL to get the latest data of each group
  • A brief discussion on group by in MySQL

<<:  CentOS7.5 installation tutorial of MySQL

>>:  Implementation of Nginx domain name forwarding https access

Recommend

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

Basic application methods of javascript embedded and external links

Table of contents Basic application of javascript...

Why MySQL should avoid large transactions and how to solve them

What is a big deal? Transactions that run for a l...

How to create a virtual environment using virtualenv under Windows (two ways)

Operating system: windowns10_x64 Python version: ...

Div picture marquee seamless connection implementation code

Copy code The code is as follows: <html> &l...

Detailed explanation of non-parent-child component communication in Vue3

Table of contents First method App.vue Home.vue H...

mysql 5.7.20 win64 installation and configuration method

mysql-5.7.20-winx64.zipInstallation package witho...

Docker data management and network communication usage

You can install Docker and perform simple operati...

Detailed explanation of the use of redux in native WeChat applet development

premise In complex scenarios, a lot of data needs...

MySQL database aggregate query and union query operations

Table of contents 1. Insert the queried results 2...

How to create a MySQL database and support Chinese characters

Let's first look at the MySQL official docume...

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

How to connect JDBC to MySQL 5.7

1. First prepare the MySQL and Eclipse environmen...

JavaScript to achieve fancy carousel effect

This article shares two methods of implementing t...