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).
精彩评论