对 mysql 中有 2 个剧集的行进行排序
Sorting rows with 2 episodes number in mysql
我有一个 table 是这样的,但问题是,每当我尝试根据他们的剧集编号从低到高按顺序设置它们时,我得到不同的结果。
id | episode
1 (Title) Episode 1
2 (Title) Episode 4
3 (Title) Episode 2
4 (Title) Episode 5-6
5 (Title) Episode 13
6 (Title) Episode 3
7 (Title) Episode 8-9
8 (Title) Episode 7
9 (Title) Episode 11-12
10 (Title) Episode 10
我试过这条线 ORDER BY LENGTH(
table.episode),
table.episode
来订购剧集。这非常有效,但如果剧集标题包含更多剧集编号,例如 Episode 5-6、Episode 8-9 和 第11-12集这一行会一直往下移
这是我得到的结果
id | episode
1 (Title) Episode 1
2 (Title) Episode 2
3 (Title) Episode 3
4 (Title) Episode 4
5 (Title) Episode 7
6 (Title) Episode 10
7 (Title) Episode 13
8 (Title) Episode 5-6
9 (Title) Episode 8-9
10 (Title) Episode 11-12
(EpisodeNumber) 的数据类型是什么,我相信它是 varchar。
不推荐按 varchar 排序,我会用 DataType int 创建一个新列并按它排序。我不确定这是否是您正在寻找的解决方案...
这样做就可以了:
SELECT *
FROM test2
ORDER BY cast( substring_index( substring_index( title, ' ', -1 ) , '-', 1 ) AS decimal )
我有一个 table 是这样的,但问题是,每当我尝试根据他们的剧集编号从低到高按顺序设置它们时,我得到不同的结果。
id | episode
1 (Title) Episode 1
2 (Title) Episode 4
3 (Title) Episode 2
4 (Title) Episode 5-6
5 (Title) Episode 13
6 (Title) Episode 3
7 (Title) Episode 8-9
8 (Title) Episode 7
9 (Title) Episode 11-12
10 (Title) Episode 10
我试过这条线 ORDER BY LENGTH(
table.episode),
table.episode
来订购剧集。这非常有效,但如果剧集标题包含更多剧集编号,例如 Episode 5-6、Episode 8-9 和 第11-12集这一行会一直往下移
这是我得到的结果
id | episode
1 (Title) Episode 1
2 (Title) Episode 2
3 (Title) Episode 3
4 (Title) Episode 4
5 (Title) Episode 7
6 (Title) Episode 10
7 (Title) Episode 13
8 (Title) Episode 5-6
9 (Title) Episode 8-9
10 (Title) Episode 11-12
(EpisodeNumber) 的数据类型是什么,我相信它是 varchar。 不推荐按 varchar 排序,我会用 DataType int 创建一个新列并按它排序。我不确定这是否是您正在寻找的解决方案...
这样做就可以了:
SELECT *
FROM test2
ORDER BY cast( substring_index( substring_index( title, ' ', -1 ) , '-', 1 ) AS decimal )