TABLE =>用户
id | category | views
1 | cat2 | 700
2 | cat1 | 3900 <== most views in cat1
3 | cat1 | 200
4 | cat1 | 800
5 | cat3 | 2900 <== most view from cat3
6 | cat1 | 800
7 | cat2 | 4900 <== most views from cat2
8 | cat1 | 900
9 | cat2 | 100
10 | cat2 | 100
// this is random table..//
我的输出:
id | category | views
2 | cat1 | 3900 <== most views in cat1
5 | cat3 | 2900 <== most view from cat3
7 | cat2 | 4900 <== most views from cat2
我想要的输出:
id | category | views
7 | cat2 | 4900 <== most views from cat2
6 | cat2 | 1000
10 | cat2 | 4000
8 | cat2 | 500
5 | cat3 | 2900 <== most view from cat3
11 | cat3 | 1000
13 | cat3 | 2500
15 | cat3 | 100
14 | cat3 | 500
我想显示该类别的行,其中最大视图的id大于4,如果不显示则不显示该行,此后类别将满足上述id> 4的条件,然后在该类别中显示该行类别明智的顺序,但最大观看次数较高的类别应首先显示,其他则依次显示
我的sql代码:
SELECT *
FROM neww
where category in (
SELECT t1.category
FROM neww t1 inner join (
SELECT category,MAX(views) AS max_view
FROM neww
GROUP BY category
) t2 on t2.max_view = t1.views and t1.id >= 4
) and id >= 4
order by category desc, views desc