select orderNo from (select (@rowNum:=@rowNum+1) orderNo , userid ,pid from TABLE,(Select (@rowNum :=0) ) b )t where t.userid=2694
然后php获得这条结果数组,取第一个元素即是排序
如果对这个sql语句有疑问,不明白,可以使用一个比较耗费性能的方法:
select userid from record
执行这条语句,获得一个数组$res
遍历前设置一个记录排序的标识 $seq=1
for(...){
$userid=$res['userid']//把第seq 个位置的学号拿出来,跟想要的学号比
if($userid==2694){
break
}
$seq++
}
//如果 这个排序标识比结果集数组大小还大,说明没这个userid的记录。
$seq就是排序
我没有测试 你可以自己试一下
select t.songid,sum(score) sumscore from(select songid,count(*)*1 score from collection group by songid
union all
select songid,count(*)*1 score from identification group by songid
union all
select songid,count(*)*0.5 score from listen group by songid) t
order by sum(score) desc limit 5
这样试试呢?
工具:SQLyog
DELIMITER $$USE `test`$$
DROP PROCEDURE IF EXISTS `p_update_student_score`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `p_update_student_score`()
BEGIN
DECLARE ids INT
DECLARE ranks INT
DECLARE stopFlag INT
DECLARE cursor_ids CURSOR FOR SELECT t.id ids,@a := @a + 1 ranks FROM (SELECT * FROM STUDENT ORDER BY score DESC) t ,(SELECT @a := 0) AS a
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET stopFlag=1
OPEN cursor_ids
REPEAT
FETCH cursor_ids INTO ids,ranks
UPDATE STUDENT SET RANK = ranks WHERE id = ids
UNTIL stopFlag END REPEAT
CLOSE cursor_ids
END$$
DELIMITER
用法:
CALL p_update_student_score结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)