Answer the question
In order to leave comments, you need to log in
How to "smartly" merge 2 tables?
Good day.
There are 2 tables - the first one contains text messages (wall, ala message wall), and the second one contains events (events, user actions - adding a photo, creating blog entries).
Now these 2 tables are arranged as follows:
We set the total limit to 20, take 10 records and one and 10 from the other, combine sorting by the time the record was created in reverse chronological order and show. But this approach is not quite correct...
How can I merge records from these 2 tables based only on the date of creation of the record? Those. if the user's last 19 actions are exactly events from events, then the final selection should contain 19 records from events and only one from wall.
Subd used PostgreSQL 9.3\9.4
Answer the question
In order to leave comments, you need to log in
Use UNION:
SELECT t1.id, t1.date, t1.name, 'wall' AS `type` FROM `tbl1` AS `t1`
UNION ALL
SELECT t2.id, t2.date, t2.event AS `name`, 'events' AS `type` FROM `tbl2` AS `t2`
ORDER BY `date` DESC
LIMIT 20
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question