内部语句中的变量SQL顺序

时间:2011-12-13 09:53:11

标签: mysql sql

我有一个像这样的MySQL查询:

SELECT * 
FROM backstage 
WHERE backstage_id IN (
    SELECT backstage_id 
    FROM visitor_counter 
    WHERE backstage_id !=0 
    GROUP BY backstage_id 
    ORDER BY COUNT( DISTINCT ( ip_address ) ) DESC 
) 
LIMIT 0 , 100

我得到了我想要的结果,但我想按COUNT( DISTINCT ( ip_address ) ) DESC的顺序排序,就像内心的问题一样。

有关如何执行此操作的任何提示?

1 个答案:

答案 0 :(得分:2)

给它一个去看看它是否能为你提供你所追求的东西:

select bs.*
from backstage bs
inner join
(
select backstage_id,count(distinct ip_address) as distIpCount
from visitor_counter
where backstage_id !=0
group by backstage_id
) vc on vc.backstage_id = bs.backstage_id
order by vc.distIpCount desc
limit 0,100;