2012-06-28 47 views
0

我的表單中有兩種類型的交易1)One way relocation deal and 2)Regular deal。我的db -Vehicles中有一個表。使用ORDER BY子句在MYSQL中排序數據

vehicles table 
===================================================== 
id, pickuplocation, returnlocation, included location 

現在,當用戶選擇One way relocation deal然後在數據庫中,只有id,pickuplocation,returnlocation將插入和coloumn included location將是無效的。而當用戶選擇Regular deal,列pickuplocation,returnlocation西港島線是空只有coloumn id and included location將set.Problem是我想用自己的location.I嘗試此查詢的整個數據進行排序 -

SELECT * 
FROM vehicles 
where 1=1 
ORDER BY pickuplocation ASC,returnlocation ASC 

但它不會給予適當結果,因爲具有包括location.how空我可以排序的所有pickuplocation,returnlocation,exculded location

+0

你想去哪裏空項才能出現? – Mark

+0

您是否想要'pickup位置','returnlocation'和'排除位置'在一列中並進行排序?你想如何處理'null'? –

+1

$條件的內容是什麼?你爲什麼要放一個'1 = 1'的條件? –

回答

0

得到了solution.I使用該查詢

(SELECT id,pickuplocation,returnlocation,deal_type FROM `vehicles` where deal_type = 1) 

UNION 

(SELECT id,included_location,excluded_location,deal_type FROM `vehicles` where deal_type = 2) 

order by pickuplocation,returnlocation 

我希望這會幫助別人誰可能會遇到在未來類似的問題。

+0

不,這對未來沒有任何幫助。 「爲什麼?」你問,因爲我們幾乎不知道這個問題。 – Adi

0

一些數據,你想要的IFNULL功能

嘗試通過

0排序
0
SELECT * 
FROM vehicles 
where 1=1 AND ($condition) 
ORDER BY pickuplocation ASC,returnlocation ASC 

假設$condition將會像field01=1field2 >=1