非常に簡単な選択が、私は混乱している:のMySQL 5.7グループ
+------+------+
| id | type |
+------+------+
| 3 | 100 |
| 2 | 101 |
+------+------+
私のクエリは次のとおりです:
MySQLバージョン
create table users (id int, type int);
insert into users values(1, 100), (2, 101), (3, 100);
mysql> select * from users;
+------+------+
| id | type |
+------+------+
| 1 | 100 |
| 2 | 101 |
| 3 | 100 |
+------+------+
が、私は結果を取得したいです5.7が返されます。
mysql> select * from (select * from users order by id desc) as t group by type;
+------+------+
| id | type |
+------+------+
| 1 | 100 |
| 2 | 101 |
+------+------+
MySQL 5.5 it期待どおりに動作します。 sql_modeがNULL
ありがとうございます。
create table users (id int, type int, level int);
insert into users values(1, 100, 1000), (2, 101, 1001), (3, 100, 1002);
mysql> select max(id), type, level from users group by type;
+---------+------+-------+
| max(id) | type | level |
+---------+------+-------+
| 3 | 100 | 1000 | <- expected 1002
| 2 | 101 | 1001 |
+---------+------+-------+
これは動作します:
mysql> SELECT t1.* FROM users t1 INNER JOIN (SELECT type, MAX(id) AS max_id FROM users GROUP BY type) t2 ON t1.type = t2.type AND t1.id = t2.max_id ORDER BY id DESC;
+------+------+-------+
| id | type | level |
+------+------+-------+
| 3 | 100 | 1002 |
| 2 | 101 | 1001 |
+------+------+-------+
検索クエリが機能します。ありがとう。 –