我想了一个效率比较差的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就是根据每个条件检索出来的都设标志位,然后计算符合条件的纪录。
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
这种高效率的没有。 只能用临时表, create temporary table 大胖的语句 order by count;
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就是根据每个条件检索出来的都设标志位,然后计算符合条件的纪录。
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
只能用临时表,
create temporary table 大胖的语句 order by count;