MySQL GROUP BY分組取欄位最大值

神諭丶發表於2016-09-20

假設有一個業務場景,需要查詢使用者登入記錄資訊,其中表結構如下:
  1. CREATE TABLE `tb` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `uid` int(11) NOT NULL,
  4.   `ip` varchar(16) NOT NULL,
  5.   `login_time` datetime,
  6.   PRIMARY KEY (`id`),
  7.   KEY (`uid`)
  8. );

再來點測試資料:
  1. INSERT INTO tb SELECT null, 1001, '192.168.1.1', '2016-01-01 16:30:47';
  2. INSERT INTO tb SELECT null, 1003, '192.168.1.153', '2016-01-01 19:30:51';
  3. INSERT INTO tb SELECT null, 1001, '192.168.1.61', '2016-01-01 16:50:41';
  4. INSERT INTO tb SELECT null, 1002, '192.168.1.31', '2016-01-01 18:30:21';
  5. INSERT INTO tb SELECT null, 1002, '192.168.1.66', '2016-01-01 19:12:32';
  6. INSERT INTO tb SELECT null, 1001, '192.168.1.81', '2016-01-01 19:53:09';
  7. INSERT INTO tb SELECT null, 1001, '192.168.1.231', '2016-01-01 19:55:34';

表資料情況:
  1. +----+------+---------------+---------------------+
  2. | id | uid  | ip            | login_time          |
  3. +----+------+---------------+---------------------+
  4. | 1  | 1001 | 192.168.1.1   | 2016-01-01 16:30:47 |
  5. | 2  | 1003 | 192.168.1.153 | 2016-01-01 19:30:51 |
  6. | 3  | 1001 | 192.168.1.61  | 2016-01-01 16:50:41 |
  7. | 4  | 1002 | 192.168.1.31  | 2016-01-01 18:30:21 |
  8. | 5  | 1002 | 192.168.1.66  | 2016-01-01 19:12:32 |
  9. | 6  | 1001 | 192.168.1.81  | 2016-01-01 19:53:09 |
  10. | 7  | 1001 | 192.168.1.231 | 2016-01-01 19:55:34 |
  11. +----+------+---------------+---------------------+


如果只需要針對使用者查出其最後登入的時間,可以簡單寫出:
  1. SELECT uid, max(login_time)
  2. FROM tb
  3. GROUP BY uid;

  4. +------+---------------------+
  5. | uid  | max(login_time)       |
  6. +------+---------------------+
  7. | 1001 | 2016-01-01 19:55:34 |
  8. | 1002 | 2016-01-01 19:12:32 |
  9. | 1003 | 2016-01-01 19:30:51 |
  10. +------+---------------------+

若還需要查詢使用者最後登入時的其他資訊,就不能用這種sql寫了:
  1. -- 錯誤寫法
  2. SELECT uid, ip, max(login_time)
  3. FROM tb
  4. GROUP BY uid;
  5. -- 錯誤寫法
這樣的語句是非SQL標準的,雖然能夠在MySQL資料庫中執行成功,但返回的卻是未知的
(如果sql_mode開啟了only_full_group_by,則不會執行成功。)

可能ip欄位會取uid分組前的第一個row的值,顯然不是所需資訊


寫法①
那麼寫一個子查詢吧:
  1. SELECT a.uid, a.ip, a.login_time
  2. FROM tb a
  3. WHERE a.login_time in (
  4. SELECT max(login_time)
  5. FROM tb
  6. GROUP BY uid);

寫法②
再或者換一個寫法:
  1. SELECT a.uid, a.ip, a.login_time
  2. FROM tb a
  3. WHERE a.login_time = (
  4. SELECT max(login_time)
  5. FROM tb
  6. WHERE a.uid = uid);


順便測了一下
在5.6以前的版本中,寫法②這條sql在大資料量的情況下,執行計劃不理想,目測效能不佳。
在5.6及以後的版本中,寫法②這條sql會快很多,執行計劃也有了改變


5.5.50:
  1. +----+--------------------+-------+------+---------------+------+---------+------+------+-------------+
  2. | id | select_type        | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
  3. +----+--------------------+-------+------+---------------+------+---------+------+------+-------------+
  4. | 1  | PRIMARY            | a     | ALL  | NULL             | NULL  | NULL      | NULL | 7    | Using where |
  5. | 2  | DEPENDENT SUBQUERY | tb    | ALL  | uid           | NULL  | NULL      | NULL | 7    | Using where |
  6. +----+--------------------+-------+------+---------------+------+---------+------+------+-------------+


5.6.30:
  1. +----+--------------------+-------+------+---------------+------+---------+------------+------+-------------+
  2. | id | select_type        | table  | type | possible_keys | key  | key_len | ref       | rows  | Extra      |
  3. +----+--------------------+-------+------+---------------+------+---------+------------+------+-------------+
  4. | 1  | PRIMARY            | a     | ALL  | NULL              | NULL | NULL      | NULL        | 7    | Using where |
  5. | 2  | DEPENDENT SUBQUERY | tb    | ref  | uid           | uid  | 4       | test.a.uid | 1    | NULL           |
  6. +----+--------------------+-------+------+---------------+------+---------+------------+------+-------------+


寫法③ 
索性直接改成join效能會更加好:
  1. SELECT a.uid, a.ip, a.login_time
  2. FROM (SELECT uid, max(login_time) login_time
  3. FROM tb
  4. GROUP BY uid
  5. ) b JOIN tb a ON a.uid = b.uid AND a.login_time = b.login_time;

當然,結果都相同:
  1. +------+---------------+---------------------+
  2. | uid  | ip            | login_time          |
  3. +------+---------------+---------------------+
  4. | 1003 | 192.168.1.153 | 2016-01-01 19:30:51 |
  5. | 1002 | 192.168.1.66  | 2016-01-01 19:12:32 |
  6. | 1001 | 192.168.1.231 | 2016-01-01 19:55:34 |
  7. +------+---------------+---------------------+

當然……如果要分組取最小值直接改對應函式和符號就行了。


作者微信公眾號(持續更新)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-2125230/,如需轉載,請註明出處,否則將追究法律責任。

相關文章