我在SQL中,写了select * from mydb where 条件1 or 条件2 or 条件3
我想按满足条件的个数来排序,应该怎么写SQL语句?

解决方案 »

  1.   

    我想了一个效率比较差的select distinct id,sum(flag) as count from (
    select *,flag=1 from mydb where 条件1
    union all
    select *,flag=1 from mydb where 条件2
    union all
    select *,flag=1 from mydb where 条件3
    ) as temp order by count就是根据每个条件检索出来的都设标志位,然后计算符合条件的纪录。
      

  2.   

    select   id,sum(flag)   as   count   from   (
    select   *,flag=1   from   mydb   where   条件1
    union   all
    select   *,flag=1   from   mydb   where   条件2
    union   all
    select   *,flag=1   from   mydb   where   条件3
    )   as   temp    group by id order   by   count
      

  3.   

    这种高效率的没有。
    只能用临时表,
    create temporary table 大胖的语句 order by count;