2010-03-17 219 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) 
相關問題