how can I sort 2 columns per schedule as if it were one

2

I have 2 columns, the first is arrival_time, the second is pick_up, the question is the following, I need to sort the results depending on what type of service it is, if it is for example arrival (arrival) you have to select the schedule of the arrival_time column, if it is an exit or interhotel service you have to select your schedule from the pick_up column, and I need to sort them by time regardless of which service is, I'll give you an example of what I'm looking for

arrival 10:00
departure 10:15
11:00 departure
arrival 11:20
interhotel 11:50

The question is that if I order it for example so ORDER BY arrival_time ASC, pick_up ASC if you order it, however it is sorted first by arrival_time and then by pick_up like this:

arrival 10:00
arrival 11:20
departure 10:15
11:00 departure
interhotel 11:50

How can I get the result I want? I searched for days on the internet and I can not find anything like that, in advance thank you very much for your support!

Here the complete query

SELECT name, pax, agency, origin, destination, internal_notes, 
departure_flight, departure_date, arrival_date, arrival_flight, 
TIME_FORMAT(arrival_time, '%H:%i') AS arrival_time, TIME_FORMAT(pick_up, 
'%H:%i') AS pick_up, service_type FROM reservas WHERE arrival_date=? OR 
departure_date=? ORDER BY pick_up, arrival_time ASC
    
asked by Fernando Dlp 05.03.2018 в 02:09
source

1 answer

2

As you identify that it is an exit or arrival, place a conditional that tells you what it is, and depending on whether it is exit, select pick_up , if it is arrival select arrival_time

SELECT ....., TIME_FORMAT(hora_programada, '%H:%i') FROM (
    SELECT ....., IF('verificar si salida', pick_up, arrival_time) AS hora_programada FROM .....
) AS t ORDER BY hora_programada
    
answered by 05.03.2018 в 02:38