2010-03-17 213 views
2

将DISTINCT包括到同样使用ORDER BY CAST(thecolumn AS int)的SQL查询中,如图所示here似乎删除了该排序功能。为什么SQL DISTINCT不能与ORDER BY CAST一起使用?

这些不能合作的原因吗? (使用sqlite与C api)

谢谢。

EDIT
入门 -

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

一行x是类型CHAR(5)

NOW

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

什么是您的新的SQL的样子?你能告诉我们吗? – CResults 2010-03-17 18:51:04

回答

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 
+0

hmmm..not happens =( – 2010-03-17 18:42:18

+0

当你直接执行它时,它是否工作,例如使用'SQLite Manager'? – 2010-03-17 18:43:51

+0

有趣 - 在你的两个例子中使用管理器给我的结果与基本的“Select yourcolumn from your_table “奇怪的是,有些东西一定会搞砸的 – 2010-03-17 19:06:24

0

这是头超晚,但通过该命令有相匹配的选择列表尽然:

select distinct cast(column as int) 
from table 
order by cast(column as int) 
相关问题