表tab 三个字段id points text 其中在points上面建了索引
比如我想查询id为123的记录的text和在tab中按points递减的顺序排在第几 改怎么写?

解决方案 »

  1.   

    select count(*) from 表tab where points>=(select points from 表tab where id=123)
      

  2.   

    tab
    id  points  text
    1   156     'asd'
    2   19      'ssdfs'
    3   27      'dfgdf'
    4   210     'sdsda'
    查询id为4的记录 输出结果‘sdsda’ 1 
      

  3.   

    select * from (
    select a.id,a.points,a.text,count(b.id) as pm from tt a left join tt b
    on a.points<=b.points 
    group by a.id,a.points,a.text) aa where id=4
      

  4.   


    呃 灰常感谢WWWWA! 我再试试优化一下 看看能不能不用子查询
      

  5.   

    select count(*)
    from tab a inner join tab b on a.points<=b.points
    where a.id=4