Mathieu Mahé
Mathieu Mahé

Reputation: 2746

Mysql order on 2 columns

I have a table 'activities' with two dates : begin_date and end_date. I don't know how get a list of activities order by begin_date and end_date.

i.e : if I have 2 activities like that:

name => 'activity1', begin_date => 1:00AM, end_date => 3:00AM

name => 'activity2', begin_date => 2:00AM, end_date => 4:00AM

And I like to get:

activity1, 1:00AM, (begin_date)

activity2, 2:00AM, (begin_date)

activity1, 3:00AM, (end_date)

activity2, 4:00AM, (end_date)

Is it possible ? How can I do that in pure SQL ?

Upvotes: 1

Views: 82

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135779

SELECT t.name, t.CombinedDate
    FROM (SELECT name, begin_date AS CombinedDate
              FROM YourTable
          UNION ALL
          SELECT name, end_date AS CombinedDate
              FROM YourTable) t
    ORDER BY t.CombinedDate

Upvotes: 2

Related Questions