I have two tables:
Comments
--------
id
comment_content
time_stamp
Activities
-----------
id
activity_content
time_stamp
And I would like to combine the results from both, ordered by their timestamp fields.
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityI have two tables:
Comments
--------
id
comment_content
time_stamp
Activities
-----------
id
activity_content
time_stamp
And I would like to combine the results from both, ordered by their timestamp fields.
Combine the two tables with UNION
and ORDER BY timestamp
. You can also add an addition (type
) column so the source of a row can be identified:
SELECT
'comment' AS type, id, comment_content AS content, time_stamp
FROM
comments
UNION ALL
SELECT
'activity', id, activity_content, time_stamp
FROM
activities
ORDER BY
timestamp ;