So, I have 2 tables that do not have a common column, and I want to save them by date column
So table1 is like:
Table 1
table 2
what i want to show is all from table1, table2 and sort 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 don’t know how to determine which element (post or comment) I use inside while (rows = mysql_fetch_assoc ()), since I have different column names.
The 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
source
share