我有这个问题:
SELECT
IP, count(*) as cnt
FROM
withdraw_update wu
JOIN
update_detail u ON wu.Update_ID = u.Update_ID
group by 1
having count(*) > 1 LIMIT 10 ;
它返回两列:
IP | Cnt
我想要对Cnt的结果进行排序(DESCENDING)
所以我试过,
SELECT
IP, count(*) as cnt
FROM
withdraw_update wu
JOIN
update_detail u ON wu.Update_ID = u.Update_ID
group by 1 DESC
having count(*) > 1 LIMIT 10 ;
哪个不做任何更改。
我也尝试过:
SELECT
IP, count(*) as cnt
FROM
withdraw_update wu
JOIN
update_detail u ON wu.Update_ID = u.Update_ID
group by 1
having count(*) > 1 ORDER BY cnt DESC LIMIT 10 ;
但 cnt 返回的列值完全不同。
答案 0 :(得分:1)
这是以您想要的方式获得相同结果的一种方法,尽管不太可能是最好的。方法
Select * from (
SELECT
IP, count(*) as cnt
FROM
withdraw_update wu
JOIN
update_detail u ON wu.Update_ID = u.Update_ID
group by 1
having count(*) > 1 LIMIT 10) sub
order by cnt desc ;
答案 1 :(得分:0)
您可以使用嵌套查询(即使我不喜欢它们,因为它们可能会影响性能)
SELECT TBL.IP, TBL.CNT
FROM
(
SELECT
IP, count(*) as cnt
FROM
withdraw_update wu
JOIN
update_detail u ON wu.Update_ID = u.Update_ID
group by 1
having count(*) > 1 LIMIT 10 ;
) AS TBL
ORDER BY TBL.CNT DESC
答案 2 :(得分:0)
这是SQL Server吗?如果是,那么我想这应该是这样的:
SELECT TOP 10
IP,
count(*) as cnt
FROM withdraw_update wu
JOIN update_detail u ON (wu.Update_ID = u.Update_ID)
HAVING COUNT (*) > 1
ORDER BY cnt DESC
希望它有所帮助!