如何在一条语句查两个条件的各10条例如:user表有几百个会员,男生若干,女生若干,不规则排序。(字段gender 1男 2女)我想在一条select里取得20个最近加入的会员,分别为男、女各10,请问用什么方法能实现?

解决方案 »

  1.   

    select * from table where gender=1 order by ... limit 10
    union all
    select * from table where gender=2 order by ... limit 10
      

  2.   

    union的效率都不高了,就没啥高的了
      

  3.   

    要加个括号语法才对吧(select * from table where gender=1 order by ... limit 10)
    union all --这里使用 Union all 
    (select * from table where gender=2 order by ... limit 10);
      

  4.   

    参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....