Skip to content
Advertisement

MySQL sort by 4 fields date and time

I have a table where there are 2 date columns and 2 time columns (arrival date, arrival time, departure date, departure time)

I want to combine the 2 date fields in one and then sort by date, arrival time, departure time.

So what I want to achieve is display the arrivals and departures of the following x days in order.

From This:

To this:

This is my query right now:

Advertisement

Answer

You first need to split arrival and departure times into two rows (e.g. in your data the 3 rows become 4). Only then you can sort them:

In the above example you must replace dates strings with STR_TO_DATE(...).

Demo on db<>fiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement