1

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.

3
  • How would you combine the values? What is common beside a time_stamp? Commented Nov 24, 2014 at 11:36
  • nothing is common, that is why they are independent.
    – pingu
    Commented Nov 24, 2014 at 11:37
  • What is your desired output? Commented Nov 24, 2014 at 11:39

1 Answer 1

2

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 ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.