GROUP_CONCAT change GROUP BY order

爱⌒轻易说出口 提交于 2019-12-01 18:07:32

Looks like GROUP_CONCAT no longer preserves the VIEW order. Is this normal?

Yes, it is normal.

You should not rely, ever, on the order in which ungrouped and unaggregated fields are returned.

GROUP_CONCAT has its own ORDER BY clause which the optimizer takes into account and can change the order in which is parses the records.

To return the first record along with GROUP_CONCAT, use this:

SELECT  m.*, gc
FROM    (
        SELECT  id, MIN(date) AS mindate, GROUP_CONCAT(tags) AS gc
        FROM    myview
        GROUP BY
                id
        ) md
JOIN    m.*
ON      m.id = md.id
        AND m.date = md.mindate

How about ordering your GROUP_CONCAT?

SELECT value1, GROUP_CONCAT(value1 ORDER BY date DESC)   
FROM table1  
GROUP BY value1;

That's the syntax you need a presume.

That is because mysql does not guarantee what exact rows will be returned for the fields that are not used in aggregation functions or wasn't used to group by.

And to be clear "mature" rdbms (such as postgre, sql server, oracle) do not allow to specify * in GROUP BY (or any fields without aggregation or that was not specified in GROUP BY) - and it is great "limitation".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!