MySQL查询限制类别数量

时间:2017-08-02 06:43:42

标签: mysql

我有一个查询按类别显示帖子。我需要显示前5个类别的结果。我的查询显示类别和类别数。查询显示每个类别中的所有类别和帖子数。

我想限制类别的显示。

我有这个问题:

SELECT qp_category, name, count(qp_category) as Total
FROM questionpapers
INNER JOIN
questioncategory
ON
questionpapers.qp_category = questioncategory.qcid
GROUP BY qp_category;

这是我的表结构

category table
--------------------------
qcid | name | active
--------------------------

post table
--------------------------------------------------------
qpid | title | qp_category | description | date | active
--------------------------------------------------------

3 个答案:

答案 0 :(得分:0)

我认为你需要的是LIMIT:

SELECT qp_category, name, count(qp_category) as Total
FROM questionpapers
INNER JOIN
questioncategory
ON
questionpapers.qp_category = questioncategory.qcid
GROUP BY qp_category
LIMIT 5;

答案 1 :(得分:0)

LIMIT应该有帮助。

SELECT qp_category, name, count(qp_category) as Total
FROM questionpapers
INNER JOIN questioncategory ON questionpapers.qp_category = questioncategory.qcid
GROUP BY qp_category
LIMIT 5;

FYI:如果SQL Server然后使用TOP,如果MySQLPostgreSQL,则使用LIMIT

答案 2 :(得分:0)

您需要在查询结尾添加“LIMIT”

  SELECT qp_category, name, count(qp_category) as Total 
FROM questionpapers
 INNER JOIN questioncategory ON questionpapers.qp_category = questioncategory.qcid 
GROUP BY qp_category LIMIT 5;