mysql排名
1.版本字段相同的也给出排名
vacc_dode相同的情况下,按照版本进行排名
select * FROM (
SELECT
vacc_code,
version,
@rownum:=@rownum+1 AS rownum,
IF(@x=concat(ou.vacc_code),@rank:=@rank+1,@rank:=1) rank,
@x:=concat(ou.vacc_code) vacc_code1
FROM (select * from vacc_info where deleted= 0 ORDER BY vacc_code,version DESC ) ou, (SELECT @rownum:=0,@rank:=0) init
) t
concat中的参数是排序字段中除去排名的字段,这里排序字段有vacc_code和version,而version是排名字段,那么只有vacc_code作为concat参数,若有多个排序的话,concat也需要拼接多个.
版本不同,版本最大的排名最前
vacc_code=4101,而且版本相同=1,也给出排名1和2
2.分组排名:
根据时间和月份进行分组,取每个月排名前200的(count值相同的排名不同)
SELECT keyword,date,count,rank
FROM
(
SELECT *,
IF(@p=date,
CASE
WHEN @s:=count THEN @r:=@r+1
END,
@r:=1 ) AS rank,
@p:=date,
@s:=count
FROM (
SELECT keyword, DATE_FORMAT(create_time,'%Y-%m') as date,count(0) as count
from search_keyword
where create_time >= '2023-01-01 00:00:00'
GROUP BY date, keyword
) t,(SELECT @p:=NULL,@s:=NULL,@r:=0) r
ORDER BY date,count desc
)s
where rank<=200;