且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MySQL中为每个组选择第一行?

更新时间:2023-01-31 14:14:55

当我写作时

SELECT AnotherColumn
FROM Table
GROUP BY SomeColumn
;

它有效.IIRC 在其他 RDBMS 这样的语句中是不可能的,因为不属于分组键的列被引用而没有任何类型的聚合.

It works. IIRC in other RDBMS such statement is impossible, because a column that doesn't belongs to the grouping key is being referenced without any sort of aggregation.

这种怪癖"与我想要的非常接近.所以我用它来得到我想要的结果:

This "quirk" behaves very closely to what I want. So I used it to get the result I wanted:

SELECT * FROM 
(
 SELECT * FROM `table`
 ORDER BY AnotherColumn
) t1
GROUP BY SomeColumn
;