我的格式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
答案 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=1
或field2 >=1