


MySQL - GROUP BY grouping to get the maximum value of the field sample code details
May 15, 2018 pm 02:14 PMMySQL - GROUP BY grouping sample code details to obtain the maximum field value:
Suppose there is a business scenario where user login record information needs to be queried, and the table structure is as follows:
CREATE TABLE `tb` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NULL, `ip` varchar(16) NOT NULL, `login_time` datetime, PRIMARY KEY (`id`), KEY (`uid`) );
Let’s get some more test data:
INSERT INTO tb SELECT null, 1001, '192.168.1.1', '2017-01-21 16:30:47'; INSERT INTO tb SELECT null, 1003, '192.168.1.153', '2017-01-21 19:30:51'; INSERT INTO tb SELECT null, 1001, '192.168.1.61', '2017-01-21 16:50:41'; INSERT INTO tb SELECT null, 1002, '192.168.1.31', '2017-01-21 18:30:21'; INSERT INTO tb SELECT null, 1002, '192.168.1.66', '2017-01-21 19:12:32'; INSERT INTO tb SELECT null, 1001, '192.168.1.81', '2017-01-21 19:53:09'; INSERT INTO tb SELECT null, 1001, '192.168.1.231', '2017-01-21 19:55:34';
Table data situation: <br/>
+----+------+---------------+---------------------+ | id | uid | ip | login_time | +----+------+---------------+---------------------+ | 1 | 1001 | 192.168.1.1 | 2017-01-21 16:30:47 | | 2 | 1003 | 192.168.1.153 | 2017-01-21 19:30:51 | | 3 | 1001 | 192.168.1.61 | 2017-01-21 16:50:41 | | 4 | 1002 | 192.168.1.31 | 2017-01-21 18:30:21 | | 5 | 1002 | 192.168.1.66 | 2017-01-21 19:12:32 | | 6 | 1001 | 192.168.1.81 | 2017-01-21 19:53:09 | | 7 | 1001 | 192.168.1.231 | 2017-01-21 19:55:34 | +----+------+---------------+---------------------+
If you only need to find out the last login time for a user, you can simply write:<br/>
SELECT uid, max(login_time) FROM tb GROUP BY uid;
+------+---------------------+ | uid | max(login_time) | +------+---------------------+ | 1001 | 2017-01-21 19:55:34 | | 1002 | 2017-01-21 19:12:32 | | 1003 | 2017-01-21 19:30:51 | +------+---------------------+
If you need to query other information about the user's last login, you cannot write it in this kind of SQL: <br/>
-- 错误写法 SELECT uid, ip, max(login_time) FROM tb GROUP BY uid; -- 错误写法
Such a statement is not SQL standard, although it can The execution is successful in the MySQL database, but the returned value is unknown <br/> (If sql_mode is turned on only_full_group_by, the execution will not be successful.)
<br/>
Maybe the ip field will take the first one before the uid group The value of row is obviously not the required information<br/>Writing 1<br/>Write a subquery:<br/>
SELECT a.uid, a.ip, a.login_time FROM tb a WHERE a.login_time in ( SELECT max(login_time) FROM tb GROUP BY uid);
Writing 2<br/>Or change the writing:<br/>
SELECT a.uid, a.ip, a.login_time FROM tb a WHERE a.login_time = ( SELECT max(login_time) FROM tb WHERE a.uid = uid);
Tested it by the way<br/>In versions before 5.6, the execution plan of this SQL statement ② is not ideal when there is a large amount of data, and the visual performance is poor. <br/>In 5.6 and later versions, writing ② this sql will be much faster, and the execution plan has also changed<br/>5.5.50:<br/>
+----+--------------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | PRIMARY | a | ALL | NULL | NULL | NULL | NULL | 7 | Using where | | 2 | DEPENDENT SUBQUERY | tb | ALL | uid | NULL | NULL | NULL | 7 | Using where | +----+--------------------+-------+------+---------------+------+---------+------+------+-------------+
5.6.30:<br/>
+----+--------------------+-------+------+---------------+------+---------+------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+------+---------------+------+---------+------------+------+-------------+ | 1 | PRIMARY | a | ALL | NULL | NULL | NULL | NULL | 7 | Using where | | 2 | DEPENDENT SUBQUERY | tb | ref | uid | uid | 4 | test.a.uid | 1 | NULL | +----+--------------------+-------+------+---------------+------+---------+------------+------+-------------+
Writing method 3<br/>The performance will be better if you directly change it to join: <br/>
SELECT a.uid, a.ip, a.login_time FROM (SELECT uid, max(login_time) login_time FROM tb GROUP BY uid ) b JOIN tb a ON a.uid = b.uid AND a.login_time = b.login_time;
Of course, the results are the same: <br/>
+------+---------------+---------------------+ | uid | ip | login_time | +------+---------------+---------------------+ | 1003 | 192.168.1.153 | 2017-01-21 19:30:51 | | 1002 | 192.168.1.66 | 2017-01-21 19:12:32 | | 1001 | 192.168.1.231 | 2017-01-21 19:55:34 | +------+---------------+---------------------+
Note: If you want to group the minimum value, just change the corresponding function and symbol directly.
The above is the detailed content of MySQL - GROUP BY grouping to get the maximum value of the field sample code details. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP?

What are the application scenarios of Java enumeration types in databases?

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging
