2012-08-16 119 views
3

我們有一個coldfusion網站,它檢索我們的類別,然後按字母順序顯示它們。按查詢順序使用特定列或按字母順序排列。 - Coldfuison

我們希望能夠通過手動排列帶有數字的「排序」列的類別,但是如果此數字等於0或空使用字母順序,則可以強制執行訂單。

所以此刻的查詢是

<cfquery name="qGetThrdCat" datasource="#request.dsn#"> 
    SELECT * 
    FROM tbl_prdtthrdcats, tbl_scnd_thrdcat_rel 
    WHERE tbl_scnd_thrdcat_rel.thrdctgry_ID = tbl_prdtthrdcats.thrdctgry_ID 
    AND tbl_scnd_thrdcat_rel.scndctgry_ID = #URL.secondary# 
    AND thrdctgry_archive = 0 
    ORDER BY thrdctgry_Name ASC 
</cfquery> 

它的工作原理,如果我嘗試

ORDER BY thrdctgry_Sort ASC 

,但我不能爲我的生命連接起來,主要歸功於我缺乏程序員技能。

任何意見將不勝感激。

+0

所以你想通過'thrdctgry_Sort'進行排序,但如果它是零或null,那麼你想按'thrdctgry_Name'進行排序? – Taryn 2012-08-16 15:06:14

+0

恰恰如此,如果我們明確地設置排序順序,它將遵循一個數字的地方,其他任何東西都將按字母順序排列。 – matthew 2012-08-16 15:55:46

回答

4

我可能誤解了問題,但你應該能夠只是排序上的兩列:

ORDER BY thrdctgry_Sort ASC, thrdctgry_Name ASC 
+0

這工作完美,謝謝你的幫助! – matthew 2012-08-20 08:10:05

2

這是您的查詢與加盟:

select * 
from tbl_prdtthrdcats p 
join tbl_scnd_thrdcat_rel s 
    on p.thrdctgry_ID = s.thrdctgry_ID 
where 
    s.scndctgry_ID = #URL.secondary# 
and thrdctgry_archive = 0 

對於排序,你可以在你的ORDER子句中使用CASE。

order by 
case 
    when isnull(thrdctgry_Sort, 0) = 0 
    then thrdctgry_Name 
    else thrdctgry_Sort 
end asc 

說實話,我完全無法理解你的排序順序,但是你可以更多地使用它。

+0

嗨,穆罕默德,謝謝你的幫助。我有一個404後,我雖然做這些改變,這裏是在查詢全 - SELECT * 從tbl_prdtthrdcats p 加入頁上tbl_scnd_thrdcat_rel小號 .thrdctgry_ID = s.thrdctgry_ID 其中 s.scndctgry_ID =#URL.secondary# 和thrdctgry_archive = 0 爲了通過 情況 當ISNULL(thrdctgry_Sort,0)= 0 然後thrdctgry_Name 別的thrdctgry_Sort 端ASC matthew 2012-08-16 16:02:47

+0

你的意思是一個HTTP錯誤404?你可以在你的SQL數據庫中檢查這個查詢嗎?你使用的是什麼數據庫引擎? – Farhan 2012-08-16 19:06:52

+0

感謝穆罕默德,但我在數據庫中嘗試了這一點,但沒有奏效,這可能是我有限的編程知識! – matthew 2012-08-20 08:09:12

0
<cfquery name="qGetThrdCat" datasource="#request.dsn#"> 
SELECT * 
FROM tbl_prdtthrdcats, tbl_scnd_thrdcat_rel 
WHERE tbl_scnd_thrdcat_rel.thrdctgry_ID = tbl_prdtthrdcats.thrdctgry_ID 
AND tbl_scnd_thrdcat_rel.scndctgry_ID = #URL.secondary# 
AND thrdctgry_archive = 0 
<cfif isNumeric(thrdctgry_Sort) and thrdctgry_Sort GT 0> 
ORDER BY thrdctgry_Sort 
<cfelse> 
ORDER BY thrdctgry_Name ASC 
</cfif> 

使用SQL是最好的選擇,但如果穆罕默德加齊的解決方案不工作這應該使用你的服務器端代碼做的伎倆。

+0

感謝您的建議,但cfif查詢只是導致了404錯誤 – matthew 2012-08-20 08:09:45