为什么SQL DISTINCT不能与ORDER BY CAST一起使用?

时间:2010-03-17 18:24:23

标签: sql c sqlite

将DISTINCT包含在同样使用ORDER BY CAST(thecolumn AS int)的SQL查询中,如图所示here似乎删除了该排序功能。

这些无法合作的原因是什么? (使用带有C api的sqlite)

感谢。

修改
开始于 -

sprintf(sql, "SELECT DISTINCT rowX FROM TableX Order By Cast(rowX As int) LIMIT 150 OFFSET %s;", Offset);

rowX是Type CHAR(5)

现在

sprintf(sql, "Select rowX FROM(Select Distinct rowX From TableX)t Order By Cast(rowX As int) LIMIT 150 OFFSET %s;", Offset);

2 个答案:

答案 0 :(得分:2)

我在sqlite中使用了以下内容,并且排序工作正常:

Select Distinct thecolumn
From your_table
Order By Cast(thecolumn As int)

您是否尝试将DISTINCT放入子查询?

Select thecolumn
From
(
  Select Distinct thecolumn
  From your_table
) t
Order By Cast(thecolumn As int)

我希望它能以这种方式运作。


还有一种方法:

Select thecolumn
From
(
  Select Distinct Cast(thecolumn As int) As thecolumn
  From your_table
) t
Order By thecolumn

答案 1 :(得分:0)

这种方式太晚了,但是顺序必须完全匹配选择列表:

select distinct cast(column as int)
from table
order by cast(column as int)