两列排序MYSQL

时间:2013-07-16 16:44:23

标签: php mysql

我正在尝试根据性别是男孩的情况在我的数据库中显示姓名。我目前正在尝试此查询,但它似乎无法正常工作。

SELECT
  Name,
  COUNT(Name),
  Gender='Boy' AS totalNumber
FROM
  finaldb
GROUP BY
  Name,
  Gender
ORDER BY
  COUNT(Name) DESC
LIMIT 5

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我认为Gender='Boy'位于错误的位置,将其置于WHERE

之后

SELECT Name, Gender, COUNT(Name) AS totalNumber FROM finaldb WHERE Gender='Boy' GROUP BY Name, Gender ORDER BY COUNT(Name) DESC LIMIT 5

阅读WHERE和SQL的好地方:

http://www.w3schools.com/sql/sql_where.asp