Is there an elegant way to have performant, natural sorting in a MySQL database?

For example if I have this data set:

  • Final Fantasy
  • Final Fantasy 4
  • Final Fantasy 10
  • Final Fantasy 12
  • Final Fantasy 12: Chains of Promathia
  • Final Fantasy Adventure
  • Final Fantasy Origins
  • Final Fantasy Tactics

Any other elegant solution than to split up the games' names into their components

  • Title: "Final Fantasy"
  • Number: "12"
  • Subtitle: "Chains of Promathia"

to make sure that they come out in the right order? (10 after 4, not before 2).

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.

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相关问答推荐

为什么我安装MySQL时不能使用3306端口?

使用MySQL工作台导出具有数据的数据库表并导入到其字段具有不同数据类型的同一表中

如果我有USER列,如何将SQL CREATE TABLE与PostgreSQL和MySQL一起使用?

计算符合字段值只能包含一种事件类型这一事实的记录

根据名称将值从一个字段复制到不同记录的同一字段

如果其中一个表为空,则 mysql 中的查询会给出 0 个结果

如何 Select 具有最新的 2 个日期字段的行?

谁能帮我优化where子句

如何创建一个判断行是否存在的mysql查询-如果存在我想插入一条新记录,否则增加一个值

如何在任何连续范围内获得最大值

MySQL 查询组按日期(12 小时间隔)

减少mysql中的值但不是负数

什么是 PyMySQL,它与 MySQLdb 有何不同?它会影响 Django 部署吗?

docker-entrypoint-initdb 中的 MySQL 脚本未执行

将 Django DB 从 SQLite 迁移到 MySQL 的最佳方法是什么?

使用 Java 通过 SSH 连接到远程 MySQL 数据库

Ubuntu 中的 MySQL JDBC jar 文件在哪里?

如何使用 XML_LOAD() 将 XML 文件导入 MySQL 数据库表;功能

电话号码和地址的mysql数据类型

如何在执行 sql 脚本时 echo 打印语句