問題描述
是否有一種優雅的方式在 MySQL 數據庫中進行高性能、自然的排序?
Is there an elegant way to have performant, natural sorting in a MySQL database?
例如,如果我有這個數據集:
For example if I have this data set:
- 最終幻想
- 最終幻想 4
- 最終幻想 10
- 最終幻想 12
- 最終幻想 12:Promathia 之鏈
- 最終幻想冒險
- 最終幻想起源
- 最終幻想戰術
任何其他優雅的解決方案,而不是將游戲名稱拆分成它們的組件
Any other elegant solution than to split up the games' names into their components
- 標題:《最終幻想》
- 數字:12"
- 副標題:Promathia 之鏈"
- Title: "Final Fantasy"
- Number: "12"
- Subtitle: "Chains of Promathia"
確保它們按正確的順序出現?(4 之后的 10,而不是 2 之前).
to make sure that they come out in the right order? (10 after 4, not before 2).
這樣做很痛苦,因為時不時會有另一款游戲打破解析游戲標題的機制(例如戰錘 40,000"、詹姆斯邦德 007")
Doing so is a pain in the a** because every now and then there's another game that breaks that mechanism of parsing the game title (e.g. "Warhammer 40,000", "James Bond 007")
推薦答案
我認為這就是為什么很多東西都按發布日期排序的原因.
I think this is why a lot of things are sorted by release date.
解決方案可能是在表中為SortKey"創建另一列.這可能是標題的經過清理的版本,它符合您為便于排序或計數器而創建的模式.
A solution could be to create another column in your table for the "SortKey". This could be a sanitized version of the title which conforms to a pattern you create for easy sorting or a counter.
這篇關于MySQL中的自然排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!