select from 3 tables order by date

Discussion in 'Databases' started by dizyn, Oct 28, 2008.

  1. #1
    Hi


    I have three tables birthdays, events, holidays I want to fetch data from all three tables order by date. Can i do this? if i can what's the way?

    Regards,
     
    dizyn, Oct 28, 2008 IP
  2. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Read in the manuals of your Database server how UNION can help you out here.
     
    chisara, Oct 28, 2008 IP
  3. sampathsl

    sampathsl Guest

    Messages:
    861
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use SQL Inner Join or alias to join the 3 tables and sort the result table using SQL ORDER BY Clause.
     
    sampathsl, Oct 31, 2008 IP
  4. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Please read the Union documentation, altough you could construct the same functionality using a join the Union is made for this type of data collection and more efficient.
     
    chisara, Nov 1, 2008 IP
  5. Ancient Dragon

    Ancient Dragon Peon

    Messages:
    192
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could try something like below, replacing everything with real table and column names. All three tables have to have a common column for the selection to work. This is using SQL available by Sybase db servers.
    
    SELECT t1.birthday, t2.event, t3.holiday, t1.date
    FROM TABLE t1, t2, t3
    WHERE t1.name = t2.name
    AND     t1.name = t3.name
    ORDER BY t1.date
    
    Code (markup):
     
    Ancient Dragon, Nov 1, 2008 IP
  6. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    (SELECT date, description FROM birthdays) UNION (SELECT date, description FROM events) UNION (SELECT date, description FROM holidays) order by date;

    Something like that
     
    chisara, Nov 2, 2008 IP
  7. bigroddy

    bigroddy Peon

    Messages:
    78
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    From the question, I'm assuming you're trying to put events/birthdays/holidays on a calendar or something, and I'm also guessing the 3 tables wouldn't have a relationship, so a union/join wouldn't work in this case.

    Why not only use 1 table for all 3 and just give it a type field (event, bday, holiday) since they would all have generally the same fields anyways and then searching/sorting by date is easy?
     
    bigroddy, Nov 2, 2008 IP
  8. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    A relationship is not needed for a UNION, for a union to work each of the selects must produce the same amount of columns with the same type.
    That is a good question because in his current design information is projected in the tablenames. While information should be stored in the tables itself.
     
    chisara, Nov 3, 2008 IP