MySQL 按计数排序()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8100239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL ORDER BY COUNT()?
提问by Jako
I have a table setup in my database with this structure (http://cl.ly/3D1D3m1O0v3d0x2j0Z0E)
我的数据库中有一个具有这种结构的表设置(http://cl.ly/3D1D3m1O0v3d0x2j0Z0E)
I have running a query through a while loop and I want to order by the count of the prof column.
我通过 while 循环运行查询,我想按 prof 列的计数进行排序。
This is what my query currently looks like, though I keep getting errors.
这就是我的查询目前的样子,尽管我不断收到错误。
$order_list = mysql_query("
SELECT COUNT(prof),
FROM prof_rating
ORDER BY COUNT(prof) ASC");
This is the warning I keep getting.
这是我不断收到的警告。
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
警告:mysql_fetch_assoc() 期望参数 1 是资源,布尔值在
回答by Bill Karwin
For what it's worth, any use of an aggregate function in the select-list means that the result set will have only one row. It makes little sense to sort a results set with a single row.
就其价值而言,在选择列表中使用聚合函数意味着结果集将只有一行。用单行对结果集进行排序毫无意义。
If you meant to get a count of ratings per distinct value of prof, you should use this:
如果您打算根据 prof 的不同值获取评分计数,则应使用以下命令:
$order_list = mysql_query("
SELECT prof, COUNT(*) AS PROFCOUNT,
FROM prof_rating
GROUP BY prof
ORDER BY PROFCOUNT ASC'");
That will output multiple rows, one row per prof value, with the count of rows for each given prof value.
这将输出多行,每个 prof 值一行,以及每个给定 prof 值的行数。
回答by John Humphreys - w00te
Alias the column name and then put that in your order by clause :)
别名列名,然后将其放在您的 order by 子句中:)
$order_list = mysql_query("
SELECT COUNT(prof) AS PROFCOUNT,
FROM prof_rating
ORDER BY PROFCOUNT ASC'");