So i have 2 tables which does not have any column in common and i want to store them by their date column
So table1 is like:
table1
- id
- post_id
- post_date
table2
- id
- comment_id
- comment_date
what i want to show is everything from table1,table2 and sort it by date
i tried something like
SELECT * FROM table1 INNER JOIN table2 ORDER BY post_date DESC, comment_date DESC
the problem is that i dont know how to identify which item(post or comment) i am using inside the while(rows = mysql_fetch_assoc()) since i have different column names.
Solution was:
SELECT * FROM (
SELECT 1 AS `table`, `col1` AS `userid`, `col2` AS `cat`, `col3` AS `item_id`, `title` AS `title`, etc... , `date` AS `date` FROM `table1`
UNION
SELECT 2 AS `table`, `col1` AS `userid`, `col2` AS `cat`, `col3` AS `item_id`, NULL AS `title`, etc... , `date` AS `date` FROM `table2`
) AS tb
ORDER BY `date` DESC
1 Answer