MySQL计算重复的行值,并且不显示重复的行名称

时间:2017-02-13 11:58:30

标签: mysql

我有表STREAMS:

+----+----------+-------------------------------------------------------------+-------------+-------------+
| id | name     | stream                                                      | server      | bouquet     | 
+----+----------+-------------------------------------------------------------+-------------+-------------+
|  1 | HOME     | ["http://ur11.com/", "http://ur12.com/"]                    | ["1", "3"]  | ["3","2"]   |
|  2 | HOME     | ["http://ur15.com/", "http://ur16.com/"]                    | ["12", "13"]  | ["31","12"]   |
+----+----------+-------------------------------------------------------------+-------------+-------------+

我有桌子观看:

+----+--------+------------+---------+
| id | stream | month      | watched |
+----+--------+------------+---------+
| 16 |      2 | 2017-02-01 |       1 |
|  2 |      1 | 2017-01-01 |       3 |
| 15 |      2 | 2017-01-01 |       4 |
+----+--------+------------+---------+

需要从查询中获取此信息:

+--------+------------+---------+
| stream | month      | watched |
+--------+------------+---------+
|      2 | 2017-02-01 |       5 |
|      1 | 2017-01-01 |       3 |
+--------+------------+---------+

我尝试使用:

SELECT DISTINCT stream, month, watched FROM watched;

这个节目都没有重复,但如何计算相同的流和显示在观看?在示例中,您看到我们有流2 - 1观看和流2 - 4观看...我需要加在一起(1 + 4)并在表格流2中显示 - 观看5 ..但我只是得到第一来自distinct ...我尝试计数,但它显示我重复....我需要分组吗?

1 个答案:

答案 0 :(得分:2)

这会产生您想要的输出。这就是你要做的事情吗?

select stream, max(month) as month, sum(watched) as watched
from watched
group by stream;