楼主帖子有歧义。integral值最高的5个用户,并且这5个用户的award值为0 
出积分最高的5个用户,但是这5个用户不从来没有中过奖的?award(中奖标志):分别为0--未中奖,1--中奖 
 
我给出求:integral值最高的5个用户,并且这5个用户的award值为0 
select  top 5 * from apply_nic where award=0 order by integral desc 

解决方案 »

  1.   

    [Quote=引用 4 楼 sanqiershiyi 的回复:]
    你考虑的很仔细,这个要求不借助游标很难弄啊,mysql游标又没玩过硬用sql的话这个语句会复杂到吐血,还是还是得分布做,不然更加繁琐。
    语句我写在这里,不一定好用,你调试看看吧。create table new_nic(select * from apply_nic where phone not in(select distinct phone from apply_nic where award=1)
    and id_card not in(select distinct id_card from apply_nic where award=1) and award=0);select * from new_nic TBN1 inner join 
    (select phone, max(TBID_ALL.MI) AS MIPH from
    (select * from new_nic as TBN
    inner join
    (select id_card AS IDC ,max(integral) as MI from new_nic GROUP BY id_card) as TBID
    on TBN.id_card=TBID.id_card and TBN.integral=TBID.integral) as TBID_ALL
    group by phone) as TBIDPH
    on TBN1.phone=TBIDPH.phone and TBN1.integral=TBIDPH.MIPH
    order by TBIDPH.MIPH limit 5;要是能用了一定要给我加分啊,写了我半个多小时,都快抓狂了。