简体   繁体   English

如何按分组依据中的其他列排序?

[英]how to order by other column in the group by?

I have the following articles mysql table: 我有以下几条mysql表:

id | date
 1 | 2013-02-16 00:00:00
 2 | 2013-02-17 00:00:00 

I want to get a comma separated list of the ids (1,2) and the last date value (2013-02-17 00:00:00) 我想以逗号分隔的ID(1,2)和最后一个日期值的列表(2013-02-17 00:00:00)

I use the following query: 我使用以下查询:

SELECT GROUP_CONCAT(id),date FROM articles ORDER BY date DESC

The query selects the first date value it encounters (2013-02-16 00:00:00), how to make it select the last one instead? 该查询选择遇到的第一个日期值(2013-02-16 00:00:00),如何使其选择最后一个呢?

Thanks 谢谢

select group_concat(id), 
       max(date) as max_date
from your_table

SQLFiddle demo SQLFiddle演示

What about use MAX : 那么使用MAX

SELECT GROUP_CONCAT(id),MAX(date) 
FROM articles 

And the fiddle: http://sqlfiddle.com/#!2/4e957/3 和小提琴: http ://sqlfiddle.com/#!2/ 4e957/3

用这个:

SELECT GROUP_CONCAT(id),date FROM articles ORDER BY date ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM