Sorting by multiple dates in one table

Discussion in 'MySQL' started by bencallis, Jul 17, 2011.

  1. #1
    We are wanting to build a recent activity table in our website which will act as a timeline for recent orders.

    The current table

    id | order date | shipping date | declined date
    1 11-01-11 19-01-11
    2 13-01-11 14-01-11
    3 15-01-11 16-01-11
    4 15-01-11 15-01-11


    The required output should be (sorted by most recent date date)

    1 19-01-11 (shipped)
    3 16-01-11 (shipped)
    4 15-01-11 (shipped)
    2 14-01-11 (declined)

    One way I have though to do this is add another field in the table 'updated date' and then simply sort by this. I am just wondering if there is a more efficient way.
     
    bencallis, Jul 17, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You don't need to add a field to the table, you just create it on the fly in the query:

    SELECT TableName.id, IF([declined date]>[shipping date],[declined date],[shipping date]) AS UpdatedDate
    FROM TableName
    ORDER BY IF([declined date]>[shipping date],[declined date],[shipping date]) DESC;
    PHP:
    Be sure to replace TableName with the actual name of your table.
     
    plog, Jul 18, 2011 IP
  3. srikanth03565

    srikanth03565 Greenhorn

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    good one dude keep posting ..
     
    srikanth03565, Jul 19, 2011 IP
  4. imraan

    imraan Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    writing a query is more efficient than creating a VIEW or JOINING TABLE.. querying is faster process..
     
    imraan, Aug 8, 2011 IP