晚上,
我正在尝试获取MySQL 中每组限制为n的行的输出。我可以让它在没有连接的情况下工作,但有了它我只是害羞。我在这里粘贴了相关表格的转储:
我正在使用的查询是:
SELECT
title, catRef, RowNum, pCat, tog
FROM
(
SELECT
title, catRef,
@num := IF(@prevCat=catRef,@num+1,1) AS RowNum,
@prevCat AS tog,
@prevCat := catRef AS pCat
FROM (select @prevCat:=null) AS initvars
CROSS JOIN
(
SELECT p.title, oi.catRef
FROM resources p
INNER JOIN placesRel v ON (p.resId = v.refId)
INNER JOIN catRel oi ON (p.resId = oi.refId)
WHERE p.status = 'live' AND v.type = 'res' AND oi.type = 'res'
) AS T
) AS U
WHERE RowNum <= 5
ORDER BY catRef
我只是无法让行数增加。或任何其他解决方案将不胜感激。
我正在寻找这样的结果:
title catRef RowNum
Title1 1 1
Title2 1 2
Title3 1 3
Title4 2 1
Title5 2 2
Title6 3 1
目前,RowNum 列始终为 1。