尝试对以下TEAM_TOTAL列描述进行排序
MATCHID TEAM_TOTAL
---------- -----------------
573 Total 112
573 Total 2 for 115
574 Total 9 for 97
574 Total 2 for 100
575 Total 9 for 129
575 Total 9 for 101
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
578 Total 6 for 193
578 Total 119
但问题是TEAM_TOTAL是varchar,有没有单独的查询方法我可以按排序的desc顺序获得结果。 此列中还有一个文本。我的想法已经用完了
结果应该像下面的结果集
一样 MATCHID TEAM_TOTAL
---------- -----------------
578 Total 6 for 193
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
575 Total 9 for 129
578 Total 119
573 Total 2 for 115
573 Total 112
575 Total 9 for 101
574 Total 2 for 100
574 Total 9 for 97
答案 0 :(得分:2)
尝试一下:
select * from t
order by substring(
team_total, locate('for', team_total) +
if(locate('for', team_total) > 0, 4, 7))+0 desc
小提琴here。
答案 1 :(得分:0)
尝试提取整数(最后一个空格后面的字符串):
-- 'Total 112' - extracts 112
SELECT SUBSTRING('Total 112', LENGTH('Total 112') - LOCATE(' ', REVERSE('Total 112')));
-- 'Total 6 for 193' - extracts 193
SELECT SUBSTRING('Total 6 for 193', LENGTH('Total 6 for 193') - LOCATE(' ', REVERSE('Total 6 for 193')));
现在,您可以将该字符串转换为数字,然后按顺序排序。
SELECT * FROM teams
ORDER BY
CAST(SUBSTRING(TEAM_TOTAL, LENGTH(TEAM_TOTAL) - LOCATE(' ', REVERSE(TEAM_TOTAL))) AS INT) DESC