业务中需要根据某个字段大小来进行数据的排名 方法如下
表设计:
CREATE TABLE `yl_solo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`isuse` tinyint(1) NOT NULL DEFAULT '1',
`estate` tinyint(1) NOT NULL DEFAULT '1',
`userid` int(11) NOT NULL DEFAULT '0' COMMENT '用户id',
`username` varchar(50) NOT NULL DEFAULT '' COMMENT '姓名',
`address` varchar(255) NOT NULL DEFAULT '' COMMENT '地区',
`specialty` varchar(255) NOT NULL DEFAULT '' COMMENT '特长',
`pictureid` int(11) NOT NULL DEFAULT '0' COMMENT '照片',
`createtime` int(11) NOT NULL DEFAULT '0' COMMENT '时间',
`makelist` int(11) NOT NULL DEFAULT '0' COMMENT '打榜了热度',
`experience` int(11) NOT NULL DEFAULT '0' COMMENT '经验',
`level` int(11) NOT NULL DEFAULT '0' COMMENT '级别1(A)2(B)3(C)4(D)5(E)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COMMENT='个人出道';
sql语句:
SELECT
userid,
username,
pictureid AS imgurl,
address,
specialty,
makelist,
rank
FROM
(
SELECT
d.*,(
@rowno := @rowno + 1
) AS rank
FROM
(
SELECT
a.userid,
a.pictureid,
a.address,
a.specialty,
a.makelist,
a.estate,
a.username
FROM
yl_solo a
ORDER BY
a.makelist DESC
) d,(
SELECT
( @rowno := 0 )) c
) e
WHERE
userid = 13589 and estate = 1
此sql是根据makelist来进行的排名
结果: