开发者

MySQL select/where statement

开发者 https://www.devze.com 2023-02-03 15:40 出处:网络
I have a webapplication linked to a mysql database with the following fields: field 1:trip_id field 2:trip_destination

I have a webapplication linked to a mysql database with the following fields:

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

In the webapplication I have a lis开发者_StackOverflowtbox based on the following:

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

How do I put this in the sql select statement?


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

You would then need to replace start_day and end_day with your periods e.g. start_day = 6 end_day=10.

Hope this helps.


in its simplest form, from your internally controlled values of the listbox ranges (and I'm not a PHP programmer to fill in the blanks), but a query could be.

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

If you are trying to get all trips, and have them pre-stamped with a 1-4 classification, I would apply a CASE WHEN

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号