如何随机后再按数量排序?tableA:
id, name, total
1,abc,10
2,def,9
3,xyz,6
4,qwe,2
5,asd,8
.........有一百个纪录请问怎样random select出来后,知道该record的total是由大至小中的第几个?例如:知道id5是排大至小的第3个
谢谢

解决方案 »

  1.   

    随便写了一下。没有做优化。你自己EXPLAIN。
    create table tableA(id int not null,`name` char(3) not null,total int not null);
    insert into tableA values
    (1,'abc',10),
    (2,'def',9),
    (3,'xyz',6),
    (4,'qwe',2),
    (5,'asd',8);create temporary table tmp(f_id int not null auto_increment primary key,id int not null,name char(3) not null,total int not null);insert into tmp(id,`name`,total) 
    select * from 
    (
    select * from tableA order by rand()
    ) T order by total desc;
    select f_id  from tmp where id = 5;
    结果:query result(1 records)
    f_id