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.

  • How would you combine the values? What is common beside a time_stamp? – Mladen Uzelac Nov 24 '14 at 11:36
  • nothing is common, that is why they are independent. – pingu Nov 24 '14 at 11:37
  • What is your desired output? – Mladen Uzelac Nov 24 '14 at 11:39
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, privacy policy and cookie policy

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