Mysql - 双组

时间:2013-12-30 16:18:59

标签: mysql sql

我跟随查询:

SELECT DATE(created) AS con_date,
       COUNT(*) AS con_per_day,
       GROUP_CONCAT(word_id)
FROM connections
WHERE created > (NOW() - INTERVAL 30 DAY)
GROUP BY con_date

计算每天添加的单词。

enter image description here

一切都很好,但我想数独特!注意word_id在最后一个raw中有14,14。这必须计为1。

我认为double group_by会削减它,但这似乎是一种错误的方法。

有什么想法吗?

3 个答案:

答案 0 :(得分:5)

使用distinct

    SELECT DATE(created) AS con_date,
           COUNT(distinct word_id) AS con_per_day,
           GROUP_CONCAT(word_id)
    FROM connections
    WHERE created > (NOW() - INTERVAL 30 DAY)
    GROUP BY con_date

答案 1 :(得分:1)

你只需要使用不同的地方,你会说些什么: 你会最终得到:   COUNT(distinct word_id)

答案 2 :(得分:1)

distinct添加到count()group_concat()

SELECT DATE(created) AS con_date,
       COUNT(distinct word_id) AS con_per_day,
       GROUP_CONCAT(distinct word_id)
FROM connections
WHERE created > (NOW() - INTERVAL 30 DAY)
GROUP BY con_date;