这个查询给出了奇怪的结果:
SELECT `user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND (`rankType` = "top5"
OR `rankType` = "top20")
ORDER BY rankType
LIMIT 0 , 30
这里是SQLfiddle .
我想要实现的是:
1)只得到5条top5排名类型的记录,20条排名类型top20的记录
2)我想以排名类型的升序
显示结果。(但如果您在演示 fiddle 中看到它显示适当,可能是它只考虑 20 和 5 中的 2
)
(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。您必须采用自然排序或其他一些方法才能正确处理。
我是一名优秀的程序员,十分优秀!