0

我有这张表,其中每个用户在表中可以有超过 1 行,所以我想找出每个用户的前两行。

我看到了这个链接并尝试了解决方案

SQL - 每种类型只给我 3 次点击

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 是否增加,但它没有增加。知道为什么吗?

4

2 回答 2

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

我认为您错过了查询的某些部分。

于 2013-06-26T03:57:33.487 回答
0

这是工作

将其包含在内联视图中

sqlfiddle 演示

于 2013-06-26T03:52:24.200 回答