2012-06-28 20 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