开发者

MySQL group by concatenated columns or functions?

This appears to work,

 SELECT CONCATENATE(col1,col2) newcol,sum(othercol) 
        FROM mytable GROUP BY newcol.

Or even

SELECT STR_TO_DATE("%Y%m%d") as newcol,sum(othercol) 
        FROM mytable GROUP BY newcol.

Looking at these, one assumes the first example produces a count for each unique combination of col1,col2 as a string, th开发者_如何学Ce second example to produce a count for each day.

Having been bitten before by mysql e.g. silently ignoring "missing" columns in group by,

Does the above actually work, or are there any hidden gotchas ?


If you want to count rows, you should use count(*) instead of sum().

Otherwise, this pattern works fine. (I personally use "GROUP BY 1" to signify grouping by the first column).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜