I think this is what you're looking for, if you want to show all tasks even if they don't have activities.
sekect task.id, task.title, activity.id, activity.title
from task
left join activity
order by task.createdat, activity.createdat desc
edit: I'll provide a short explanation that might help you understand a little more.
This statement basically tells the database to grab all the information in the specific columns "id" and "title" in the task table and the activity table - this is the sekect task.id, task.title, activity.id, activity.title
clause.
Any query will always search at least one table, using the from
keyword. In this case, we used from task
, which would search the task table. However, for this example, you needed a union of two tables, requiring the use of the join
keyword. Using join
allows us to search more than one table, in multiple ways. We used a left join
in this case; comment if you'd like a further explanation about this.
Finally, we simply ordered the data by the task creation date, and then the activity creation date, and specified descending order (oldest first, newest last).
Note that, in general, using select *
isn't the best idea. It may satisfy your needs now, but if you're going to build more queries, you'll want to specify the columns you want to display, as in my example.