gpt4 book ai didi

php - 我如何对下表进行排序并获得前 5-5 条记录,前 20-20 条记录?

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:44 24 4
gpt4 key购买 nike

这个查询给出了奇怪的结果:

    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。您必须采用自然排序或其他一些方法才能正确处理。

关于php - 我如何对下表进行排序并获得前 5-5 条记录,前 20-20 条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com