我创建了以下查询:
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
,但这根本没有给我任何结果,所以显然我错了。我在这里缺少什么?
答案 0 :(得分:0)
我找到了解决方案。显然我应该像这样使用CAST函数:
cast(substring_index(group_concat(id ORDER BY id DESC), ',', 1) as unsigned) AS newid