MySQL CAST作为无符号不起作用

时间:2018-06-08 17:17:40

标签: mysql casting

我创建了以下查询:

SELECT DISTINCT
model,
substring_index(group_concat(id ORDER BY id DESC), ',', 1) AS newid, 
substring_index(group_concat(mainimage ORDER BY id DESC), ',', 1) AS newmainimage
FROM `example-table`
GROUP BY model
ORDER BY newid DESC;

问题是此查询对变量newid进行排序,如下所示:99,9,87,121,11,而我希望看到:121,99,87,11,9。

我认为答案是改变ORDER BY newid DESC中的ORDER BY CAST(newid AS UNSIGNED) DESC,但这根本没有给我任何结果,所以显然我错了。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。显然我应该像这样使用CAST函数:

cast(substring_index(group_concat(id ORDER BY id DESC), ',', 1) as unsigned) AS newid