这是一些测试数据。选择一些不同的列
+----+------+-------+---------+---------+
| id | type | param | enabled | account |
+----+------+-------+---------+---------+
| 1 | test | a | 1 | null |
| 2 | asdf | b | 1 | null |
| 3 | test | c | 1 | 34 |
| 4 | test | d | 0 | 34 |
| 5 | asdf | e | 1 | null |
+----+------+-------+---------+---------+
我想选择最新的行,其中“类型”和“帐户”是唯一的。
例如,对于测试表,我想要的结果:
+----+------+-------+---------+---------+
| id | type | param | enabled | account |
+----+------+-------+---------+---------+
| 1 | test | a | 1 | null |
| 4 | test | d | 0 | 34 |
| 5 | asdf | e | 1 | null |
+----+------+-------+---------+---------+
我试着按组的变化:
SELECT * FROM test GROUP BY type, account
出于某种原因,让我这样的:
+----+------+-------+---------+---------+
| id | type | param | enabled | account |
+----+------+-------+---------+---------+
| 1 | test | a | 1 | null |
| 4 | test | d | 1 | 34 | <- note that enabled is taking on an incorrect value.
| 5 | asdf | e | 1 | null |
+----+------+-------+---------+---------+
什么是正确的方法来做到这一点?
您试过'SELECT DISTINCT * FROM test'吗? –
DISTINCT适用于所有选定的列。所以在这个例子中,它会选择'id','type','param','enabled'和'account'都是唯一的。我只想'类型'和'帐户'是唯一的。 – ieatpizza