MySQL查询ORDER BY行NUMER?

我有票MySQL表。 有一个id一个project_idvote场(是1,如果一个特定的项目进行表决)。 现在我想生成这些条目排名。 有没有办法拿到票的每个PROJECT_ID的数量,并自动通过一个项目的真票数项与单个MySQL查询排序? 或者你知道一个PHP的方法吗?

例如

ID | Project ID | Vote ----------------------- 1 | 2 | 1 2 | 2 | 1 3 | 1 | 1

==>

项目十五分。 2有2票

项目十五分。 1有1投票

提前致谢!

--------------解决方案-------------

SELECT
`project_id`, SUM(`vote`) AS vote_count
FROM
`table_name`
GROUP BY `project_id`
ORDER BY vote_count DESC

走样的总和将帮助你找到在PHP列后,如果您获取作为关联数组

Select project_id, Sum( vote ) project_vote
From votes
Group By project_id
Order By project_vote Desc

GROUP BY可以让你拿到票每PROJECT_ID的总和,在ORDER BY DESC把最高的票数第一。

使用GROUP BY

SELECT project_id, SUM(vote) as votes
FROM table
GROUP BY project_id
ORDER BY votes DESC

尝试这个:

SELECT
ProjectID,SUM(Vote)
FROM YourTable
GROUP BY ProjectID
ORDER BY Sum(Vote) DESC

分类:php 时间:2015-03-15 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.612 (s). 10 q(s)