我有2张桌子。我通过desc格式按顺序得到结果。 现在我想以相同的订单格式显示信息。但我无法做到这一点。
select *
from table1
where field in (select *
from table2
where StartDate > '2011-11-01'
AND StartDate < '2011-11-30'
group by field1
order by count(field1) desc );
内部查询按降序排序,但与外部查询一起使用时,订单将丢失。
答案 0 :(得分:0)
in
子句不保留排序,我很惊讶MySQL甚至允许这样:)
一种解决方案是计算子查询中的计数,并使用它来订购:
select *
from table1 t1
join (
select field1
, count(field1) as Field1Count
from table2
where StartDate > '2011-11-01'
and StartDate < '2011-11-30'
group by field1
) t2
on t1.field1 = t2.field1
order by
t2.Field1Count desc