2011-01-10 133 views
0

我已經連接到MySQL數據庫具有以下字段的web應用:MySQL的選擇/ where語句

field 1:trip_id 
field 2:trip_destination 
field 3:trip_description 
field 4:trip_duration 

在web應用我有基於以下列表框:

ListBox value =1: trip duration 1 - 5 days 
ListBox value =2: trip duration 6 - 10 days 
Listbox value =3: trip duration 11 -20 days 
ListBox value =4: trip duration over 20 days 

怎麼辦我把這個在sql select語句中?

+0

你如何存儲TRIP_DURATION像一個約會我會申請一個CASE ?你想要1個SQL語句還是4個? – Navi 2011-01-10 11:30:38

回答

0
SELECT * FROM trip_table WHERE trip_duration BETWEEN start_day-1 AND end_day+1; 

然後,您需要將start_day和end_day替換爲您的句點。 start_day = 6 end_day = 10。

希望這會有所幫助。

0

以其最簡單的形式,從你內部控制的列表框範圍內(並且我不是一個PHP程序員來填空),但是查詢可能是這樣。

select * 
    from TripTable 
    where trip_duration >= ?MinimumDays 
    AND trip_duration <= ?MaximumDays 

如果你試圖讓所有行程,並與他們1-4分級預蓋章,當

select *, 
     case 
     when trip_duration >= 1 and trip_duration <=5 then 1 
     when trip_duration >= 6 and trip_duration <=10 then 2 
     when trip_duration >= 11 and trip_duration <=20 then 3 
     when trip_duration > 20 then 4 
     end as TripDurationType 
    from 
     TripTable