Mysql以char数据类型排序,并带有数字和字符的组合

时间:2016-06-14 05:00:34

标签: mysql sorting

在mysql表字段player_number中接受3个字符 - 它可以只接受数字,仅接受字母或两者的组合,最多3个字母。 表格的条目如下,

 TR 
 11
 1
 2
 222
 A
 AA

有没有快速的方法来显示排序的“player_number”结果如下,

首选订单

 1
 2
 11
 222
 A
 AA
 TR

替代方案:

  A
  AA
  TR
  1
  2
  11
  222

2 个答案:

答案 0 :(得分:2)

为了获得第一个首选订单:(首先是数字然后是字母)

SELECT 
*
FROM YOUR_TABLE
ORDER BY 
CASE WHEN player_number REGEXP '[0-9]+' THEN CAST(player_number AS UNSIGNED) END , player_number

为了获得替代首选订单:(先写信然后编号)

SELECT 
*
FROM YOUR_TABLE
ORDER BY CAST(player_number AS UNSIGNED),player_number

答案 1 :(得分:1)

尝试以下内容;)

malformed_data = '{"malformed":"json"}'
test_response = self.client.post(self.url_create, malformed_data, 
                                 content_type="application/json")

<强> SQLFiddle DEMO HERE