我有这个表,每个用户在表中可以有多行,所以我想找出每个用户的前两行。
我看到了这个链接并尝试了解决方案
SQL - Give me 3 hits for each type only
SELECT user_fk, order_number, @num := IF( @group = user_fk, @num +1, 1 ) AS row_number, @group := user_fk AS dummy
FROM summary
ORDER BY `user_fk` ASC
我使用上面的查询来查看row_number是否递增,但它没有。知道为什么吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
SELECT user_fk, order_number from
(SELECT user_fk, order_number, @num := IF( @group = user_fk, @num +1, 1 ) AS row_number, @group := user_fk AS dummy
FROM summary
ORDER BY `user_fk` ASC) as x
where row_number<=2
我认为你错过了部分查询。