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,
Use SQL Inner Join or alias to join the 3 tables and sort the result table using SQL ORDER BY Clause.
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.
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):
(SELECT date, description FROM birthdays) UNION (SELECT date, description FROM events) UNION (SELECT date, description FROM holidays) order by date; Something like that
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?
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.