590
社区成员
发帖
与我相关
我的任务
分享
select * from
(
select [Id],[Mid],[Name],[Picture],[Status],[Hot],[Tops],[CreateTime],ids=ROW_NUMBER()
over(partition by Mid order by CreateTime desc) from Product where Mid in (5,7,6,13)
)a where ids<15 and [Status]>0 and [Tops]>0 order by CreateTime desc
SELECT *
FROM ( SELECT [Id] ,
[Mid] ,
[Name] ,
[Picture] ,
[Status] ,
[Hot] ,
[Tops] ,
[CreateTime] ,
ids = ROW_NUMBER() OVER ( PARTITION BY Mid ORDER BY CreateTime DESC )
FROM Product
WHERE Mid IN ( 5, 7, 6, 13 )
AND [Status] > 0
AND [Tops] > 0
) a
WHERE ids <= 14
AND Mid IN ( 5, 7 )
OR ids <= 6
AND Mid IN ( 6, 13 )
ORDER BY CreateTime DESC;
create temporary table Tab10(ID int unique auto_increment,Score int);
#drop table Tab10
insert into Tab10(Score) values(5),(10),(15);
select
*
from (
select
a.*,@rowcount:=if(a.Score=@Score,@rowcount+1,1) as RN,@Score:=a.Score
from (select * from Tab10 order by score,ID) as a) as t
where RN<=2 and Score=5 or RN<=3 and Score=10 or RN<=5 and Score=15;
显示结果
# ID, Score, RN, @Score:=a.Score
'1', '5', '1', '5'
'4', '5', '2', '5'
'2', '10', '1', '10'
'5', '10', '2', '10'
'8', '10', '3', '10'
'3', '15', '1', '15'
'6', '15', '2', '15'
'9', '15', '3', '15'
'12', '15', '4', '15'
'15', '15', '5', '15'
select * from
(
select [Id],[Mid],[Name],[Picture],[Status],[Hot],[Tops],[CreateTime],@rowcount:=if(Mid=@Mid,@rowcount+1,1) as ids,@Mid=Mid from Product where Mid in (5,7,6,13) ORDER BY Mid,CreateTime des
)a where ids<15 and [Status]>0 and [Tops]>0 order by CreateTime DESC