2013-05-18 64 views
-1
"id" "type" "parent" "country" "votes" "perCent" 
"4177" "3"  "4176"  "US"  "105" "0"// total of all the below 
"4178" "10" "4177"  "US"  "15" "0" 
"4179" "10" "4177"  "US"  "50" "0" 
"4180" "10" "4177"  "US"  "25" "0" 
"4181" "10" "4177"  "US"  "5"  "0" 
"4182" "10" "4177"  "US"  "10" "0" 

在MySQL中,是有可能選擇具有的選票10%的行。在這裏,id = 4177擁有該類別的總票數。基於此,可以選擇type = 10 and parent = 4177和哪個有10% or more的選票?如何選擇10%票數的行?

+0

有什麼問題。你做了什麼? –

+0

'從表格中選擇id,其中type = 10,votes> =總票數的10%'si我正在做的事情。總票數在'id = 4177'。可能做這樣的事情? – Norman

+0

如果總投票數是4177,那麼投票總數的10%是417.7。我是對的嗎? –

回答

1

檢查SQLFiddle

SELECT * 
FROM Votestable v 
WHERE v.votes > (SELECT 
        votes 
       FROM Votestable v1 
       WHERE v1.id = v.parent) * 0.1 
1
SELECT 
* 
FROM myTable v 
WHERE 
v.Type = 10 
AND 0.1 < v.Votes/(SELECT MAX(m.votes) FROM myTable m WHERE m.Type = 3 AND m.Id = t.Parent) 
0

試試這個: -

select id from table where type =10 and votes>=(votes/100)*10; 
+0

這將如何工作?查看其他答案。 – Norman

+0

我認爲計算是可以的。 –

0

既然你提到105總票數,

select * , ((votes * 100)/105) as percent 
from votesTable 
where ((votes * 100)/105) > 10.0 and type=10 and parent = 4177