0

对不起,伙计们,我试过了,但我不得不问。我不会带着这个去任何地方。

Here's my table `likes`:
"id"    "type"  "parent" "country" "votes"
"1"          "1"    "0" "US"    NULL
"2"          "2"    "1" "US"    NULL
"3"          "3"    "2" "US"    NULL
"4"          "10"   "3" "US"    "5"
"5"          "10"   "3" "US"    "15"
"6"          "10"   "3" "US"    "25"
"7"          "3"    "2" "US"    NULL
"8"          "10"   "7" "US"    "40"
"9"          "10"   "7" "US"    "25"
"10"     "10"   "7" "US"    "60"
"19"     "3"    "1" "US"    NULL
"20"     "10"   "19"    "US"    "10"
"21"     "10"   "19"    "US"    "20"
"22"     "10"   "19"    "US"    "30"

从此,我试图select distinct parent , sum votes where type = 10 and country = 'us'

我试过这个:SELECT SUM(likes.votes) FROM (SELECT DISTINCT parent, votes FROM likes where type = 10) likes;但我最终得到了非常奇怪的结果。

我怎样才能做到这一点?

我想要得到的结果是这样的:

parent | total
3      | 45
7      | 125
9      | 60
4

3 回答 3

1

检查SQL 小提琴

select distinct parent, sum(votes) from likes 
where type = 10 and country = 'us' GROUP BY parent;
于 2013-05-09T05:09:55.120 回答
0
select parent, sum(votes) ...
group by parent

??

于 2013-05-09T04:50:41.490 回答
0

像这样试试

SELECT parent,sum(votes) as total FROM `likes` 
WHERE type = 10 GROUP BY parent ORDER BY parent ASC
于 2013-05-09T04:53:42.613 回答