Reebal
Reebal

Reputation: 59

Two different post types in one query

I have two table in the database:

posts table

id|name|message|time

notification table

id|name|initiator|type|message|time

What I want is two output both of the tables mix not one after another.

something like this:

Simon: Reebal want to be friends? 5 minutes ago
Reebal: Yeah sure. 4 minutes ago
Simon and Reebal are friends now (notification)
Reebal: How are you? 2 minutes ago
Simon: Fine and you? 1 minute ago

and not like this:

Simon and Reebal are friends now (notification)
Simon: Reebal want to be friends? 5 minutes ago
Reebal: Yeah sure. 4 minutes ago
Reebal: How are you? 2 minutes ago
Simon: Fine and you? 1 minute ago

Is it possible to do this? I hope you understand what I mean? And please no full code, just a advice to the right way.

Upvotes: 1

Views: 57

Answers (2)

Moiz Shafqat Husain
Moiz Shafqat Husain

Reputation: 219

Advice :

You should use union in both queries to get the result.

UNION keyword

Upvotes: 1

Manoj Sharma
Manoj Sharma

Reputation: 1465

You can use UNION for this.

select * from (
    SELECT name, '' as initiator, '' as type, message, time from post
    UNION
    SELECT name, initiator, type, message, time from notification
) as data order by time desc

This should work for you

Upvotes: 0

Related Questions