我想返回投票最多的前 5 个项目,按投票数排序。我想最好的方法是将每一行的votes_up
and相加votes_down
,然后取其中的前 5 个并将其与另一个表连接起来。
这是我现有的表,Items.ID 映射到 Votes.item_ID:
Items
+----+--------+---------+
| ID | name | site_ID |
+----+--------+---------+
| 10 | box | 111 |
| 11 | hammer | 222 |
| 12 | drill | 333 |
| 13 | nail | 444 |
+----+--------+---------+
Votes
+----+---------+----------+------------+
| ID | item_ID | votes_up | votes_down |
+----+---------+----------+------------+
| 1 | 10 | 25 | 20 |
| 2 | 11 | 200 | 100 |
| 3 | 12 | 100 | 50 |
| 4 | 13 | 50 | 20 |
+----+---------+----------+------------+
这些是我想要返回的结果:
+--------+-------+
| name | votes |
+--------+-------+
| hammer | 100 |
| drill | 50 |
| nail | 30 |
| box | 5 |
+--------+-------+