开发者

mysql group concat

how can I group concat? here is my query

select t.date, group_concat(count(*)) from table1 t
group by t.date

but it returns error "Inval开发者_运维知识库id use of group function"

If I use query like this

select t.date, count(*) from table1 t
group by t.date

then it returns following output but I want to group_concat this output

2011-01-01  100
2011-01-02  97
2011-01-03  105


SELECT GROUP_CONCAT(`cnt` ORDER BY `date`) 
FROM (
    SELECT t.`date`, COUNT(*) AS `cnt`
    FROM `table1` t
    GROUP BY t.`date`
) d


Do you want to count the number of date rows group by date?

I use this statement to count number of invoice items per date in a specific month.

SELECT date(i.Date), count(*) cnt
FROM invoice i
WHERE MONTH(i.Date) = 3
GROUP BY date(i.Date)

This will group all dates that are the same. Is this what you meant?

I use GROUP_CONCAT for subqueries returning more than one row.

* EDIT * OUPS, saw that my suggestion was the same as your already tried. Then I don't understand what you are looking for. Can you please show an example of desired result?


You want something like this :

SELECT GROUP_CONCAT(n) groupN
FROM (SELECT COUNT(*) n
      FROM table1
      GROUP BY date) tmp

But that's not the role of the RDBMS! Cosmetic is the role of your application language (PHP, Python, Ruby, whatever), your query should only select your data, period. Therefore, GROUP_CONCAT is not the solution in this case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜