gpt4 book ai didi

Mysql:使用 Limit 和 Con

转载 作者:行者123 更新时间:2023-11-29 02:17:57 24 4
gpt4 key购买 nike

我得到了这个查询,它为我提供了特定国家/地区的所有评级:

SELECT DISTINCT Country, GROUP_CONCAT(rating ORDER BY rating DESC ) AS Ranking
FROM final
group by Country
ORDER BY Country;

表格是这样的:

"Country"   "Rating"    "Title" 
" zimbabwe" "0" "Biology Study Guide"
" zimbabwe" "0" "Impact: A Guide to Business Communication (Esl English 2nd Language Series)"
" zimbabwe" "0" "Wayside School Gets a Little Stranger (Wayside School)"
" ysa" "5" "Side effects"
" ysa" "9" "The Last Precinct"
" ysa" "0" "The Horse Whisperer"
" x" "0" "The Evi
" yugoslavia" "0" "Wild Animus"
" yugoslavia" "8" "Complete Works of William Shakespeare (Wordsworth Royals Series)"
" yugoslavia" "3" "Wild Animus"

我遇到了两个问题。我试图将每个国家/地区的收视率限制在 10 个以内。我在 DESC 之后添加了 LIMIT 10 并且出现语法错误。

第二个问题是,在降序排列中,10 小于 9,因为它以 1 开头。我该如何处理?

最佳答案

为了限制GROUP_CONCAT(),您可以使用SUBSTRING_INDEX() 来剪切需要的元素。要将字符串作为数字排序,您可以为此使用 CAST()。所以你的查询应该是这样的:

SELECT DISTINCT
Country,
SUBSTRING_INDEX(
GROUP_CONCAT(rating ORDER BY CAST(rating AS UNSIGNED) DESC), ',', 10
) AS Ranking
FROM
final
GROUP BY
Country
ORDER BY
Country;

关于Mysql:使用 Limit 和 Con,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494805/

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