Hello and thank you in advance,
您好,并提前感谢您,
So I have a table lets call it userrating and inside this table I have some fields, one of which is called userid.
所以我有一个表让我们称之为用户,在这个表中我有一些字段,其中一个叫做userid。
Below I will demonstrate to you how my table looks like.
下面我将向您展示我的桌子是怎样的。
userid | rating1 | rating2 | rating3 | rating4 | rating5
-----------------------------------------------------------
100 | 1 | 0 | 0 | 0 | 0
101 | 0 | 0 | 1 | 0 | 0
101 | 0 | 0 | 0 | 0 | 1
112 | 0 | 1 | 0 | 0 | 0
100 | 0 | 0 | 0 | 1 | 0
101 | 1 | 0 | 0 | 0 | 0
What I want to do is get all the rows for the same userid's.
我想要做的是获取相同用户ID的所有行。
I want to get the 2 occurances for 100, 3 occurances for 101 and 1 occurance for 112.
我想得到100次出现,3次101次出现,1次112次出现。
1 个解决方案
#1
Try this
SELECT UserId, SUM(rating1),SUM(rating2),SUM(rating3),SUM(rating4),SUM(rating5)
From userrating Group By UserID
Also check it yourself here on fiddle - http://sqlfiddle.com/#!9/598b9/3
也可以在这里自己检查一下 - http://sqlfiddle.com/#!9/598b9/3
#1
Try this
SELECT UserId, SUM(rating1),SUM(rating2),SUM(rating3),SUM(rating4),SUM(rating5)
From userrating Group By UserID
Also check it yourself here on fiddle - http://sqlfiddle.com/#!9/598b9/3
也可以在这里自己检查一下 - http://sqlfiddle.com/#!9/598b9/3