![](/img/trans.png)
[英]How can I get the top 20 city names with Google Analytics?
[英]How do I sort the following table & get those top5- 5 recoreds,top20- twenty records?
该查询给出奇怪的结果:
SELECT `user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND (`rankType` = "top5"
OR `rankType` = "top20")
ORDER BY rankType
LIMIT 0 , 30
这里是SQLfiddle 。
想要我尝试实现的是:
1)仅获得top5等级类型的5条记录,top20等级类型的20条记录
2)我想ascending order
等级类型的 ascending order
显示结果。(但是,如果您在演示小提琴中看到它表示合适, may be it is only considering 2 from 20 & 5
)
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5"
ORDER BY rankType
LIMIT 0, 5)
union
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20"
ORDER BY rankType
LIMIT 0, 20)
如果以后要添加另一组排序/过滤列,请将其全部包装成类似
select * from ( /* previous query goes here */ ) tt
where id > 100
order by id
请注意, ranktype
是varchar,因此按字典顺序进行排序,因此top20 <top5。 您必须采用自然排序或其他方法才能正确处理。
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5" limit 5
union
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20" limit 20
您的结果实际上是按升序排列的,因为在字符串比较中,列rank_type属于varchar
类型,top20比top5优先。
如果您只想在top5和top20之间进行交易,那么肮脏的解决方案可能是:
ORDER BY rankType desc
不执行两个查询并对其进行UNIONing的一种可能性:
ORDER BY FIND_IN_SET(rankType,'top5,top20')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.