0

尝试对以下 TEAM_TOTAL 列 Desc 进行排序

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
4

2 回答 2

2

试试这个:

select * from t
order by substring(
    team_total, locate('for', team_total) + 
        if(locate('for', team_total) > 0, 4, 7))+0 desc

在这里拉小提琴。

于 2012-04-21T08:41:54.573 回答
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
于 2012-04-21T08:23:41.773 回答