使用MYSQL中的ORDER BY子句对数据进行排序

时间:2012-06-28 09:24:03

标签: php mysql sql

我的格式1)One way relocation deal and 2)Regular deal有两种类型。我的db -Vehicles中有一个表。

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

现在,当用户选择One way relocation deal然后在数据库中时,只会插入id,pickuplocation,returnlocation,而列表included location将为空。当用户选择Regular deal时,列pickuplocation,returnlocation将为空,只有coloumn id and included location将设置。问题是我想使用他们的位置对整个数据进行排序。我试过这个查询 - < / p>

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

但它不会给出正确的结果,因为包含location.how中的某些数据为null,我可以为所有pickuplocation,returnlocation,exculded location

排序

3 个答案:

答案 0 :(得分:0)

您需要IFNULL功能

尝试按

排序
 IFNULL(returnlocation, includedlocation)

答案 1 :(得分:0)

得到了解决方案。我使用了这个查询

(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

我希望这将有助于将来可能遇到类似问题的人。

答案 2 :(得分:0)

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

假设$condition类似于field01=1field2 >=1