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

Detailed tutorial on installing Mysql5.7.19 on Centos7 under Linux

1. Download MySQL URL: https://dev.mysql.com/down...

Detailed explanation and extension of ref and reactive in Vue3

Table of contents 1. Ref and reactive 1. reactive...

Summary of using MySQL isolation columns and prefix indexes

Table of contents Isolate Data Columns Prefix Ind...

How to introduce img images into Vue pages

When we learn HTML, the image tag <img> int...

Common scenarios and avoidance methods for index failure in MySQL

Preface I have read many similar articles before,...

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

How does Vue implement communication between components?

Table of contents 1. Communication between father...

How to use async await elegantly in JS

Table of contents jQuery's $.ajax The beginni...

HTML meta viewport attribute description

What is a Viewport Mobile browsers place web page...

A simple method to implement scheduled backup of MySQL database in Linux

Here are the detailed steps: 1. Check the disk sp...

HTML markup language - table tag

Click here to return to the 123WORDPRESS.COM HTML ...

How to install PHP7 Redis extension on CentOS7

Introduction In the previous article, we installe...

Very practical Tomcat startup script implementation method

Preface There is a scenario where, for the sake o...

Use the njs module to introduce js scripts in nginx configuration

Table of contents Preface 1. Install NJS module M...