根据视图计数,在两列上按每个用户组选择3条记录

时间:2014-06-12 08:00:20

标签: mysql sql greatest-n-per-group

我一直陷入复杂的MySQL查询中。 这是我的表:

+---------------------------------------------------+
| id | user_id | category_id | post_id | view_count |
+---------------------------------------------------+
| 1  | 23      | 5           | 213     | 0          |
| 2  | 23      | 5           | 214     | 0          |
| 3  | 23      | 5           | 215     | 0          |
| 4  | 23      | 5           | 216     | 0          |
| 5  | 23      | 6           | 217     | 0          |
| 6  | 23      | 6           | 218     | 0          |
| 7  | 23      | 6           | 219     | 0          |
| 8  | 23      | 6           | 220     | 0          |
| 9  | 55      | 13          | 221     | 0          |
| 10 | 55      | 13          | 222     | 0          |
| 11 | 55      | 16          | 223     | 0          |
| 12 | 55      | 16          | 234     | 0          |
| 13 | 55      | 22          | 235     | 0          |
| 14 | 55      | 22          | 256     | 0          |
| 15 | 55      | 22          | 261     | 0          |
| 16 | 62      | 13          | 272     | 0          |
| 17 | 62      | 13          | 273     | 0          |
| 18 | 62      | 24          | 277     | 0          |
| 19 | 62      | 24          | 278     | 0          |
| 20 | 62      | 24          | 288     | 0          |
| 21 | 62      | 31          | 289     | 0          |
| 22 | 62      | 31          | 290     | 0          |
+---------------------------------------------------+

现在我希望每个user_id我想要3行数据,但每行应该有不同的category_id,如下面的结果集:

+--------------------------------------+
| id | user_id | category_id | post_id |
+--------------------------------------+
| 1  | 23      | 5           | 213     |
| 5  | 23      | 6           | 217     |
| 9  | 55      | 13          | 221     |
| 11 | 55      | 16          | 223     |
| 16 | 62      | 13          | 272     |
| 18 | 62      | 24          | 277     |
+--------------------------------------+

我使用的查询如下:

SELECT id,user_id,category_id,post_id,view_count,rank 
FROM ( 
    SELECT tt.* ,@rank:= CASE WHEN @group = user_id THEN @rank + 1 ELSE 1 END rank,@group:= tt.user_id 
    FROM ( 
         SELECT id,user_id,category_id,post_id, view_count 
         FROM product_table 
         WHERE view_count=(SELECT MIN(view_count) FROM product_table
    ) 
    GROUP BY user_id,category_id 
    ORDER BY user_id,category_id 
) tt 
JOIN (SELECT @rank:=0,@group:=0) t1) new_t 
WHERE rank <=3 LIMIT 9

@ m-khalid-junaid在我之前的问题中向我建议了这个查询:SELECT 3 records per user group by on two columns我调整了该查询以合并MIN(view_count)值。 我现在所做的是在检索结果集后,我将view_count增加1 但是这个查询运行如下:

if "User 1" has 6 posts in 3 categories, having each category contain 2 posts  
And  
if "User 2" has 9 posts in 3 categories, having each category containing 3 posts  
And  
if "User 3" has 7 posts in 3 categories, having 2 categories containing 2 posts and the last category which has 3 posts  

然后SQL重复运行时使用以下循环:

Cycle 1:  
Show first 3 posts of "User 1" from each of the category [1-3]  
Show first 3 posts of "User 2" from each of the category [1-3]  
Show first 3 posts of "User 3" from each of the category [1-3]  

Cycle 2:  
Show next 3 posts of "User 1" from each of the category [4-6]  
Show next 3 posts of "User 2" from each of the category [4-6]  
Show next 3 posts of "User 3" from each of the category [4-6]  

Cycle 3:  
Show next 3 posts of "User 2" from each of the category [7-9]  
Show remaining 1 post of "User 3" from last category [7]  

我希望如果没有更多&#34;用户1&#34;帖子在&#34;周期3&#34;中可用,然后获取周期1中使用的帖子&#34;用户1&#34;在&#34;周期3&#34;只有1个帖子&#34;用户3&#34;因此,在这种情况下,我希望第3周期使用最后1个帖子,并为&#34;用户3&#34;提取第1周期的第2个帖子。请注意,这些帖子现在将在上一个周期中将view_count递增为1。

我希望我能正确解释。

0 个答案:

没有答案