Radu
Radu

Reputation: 626

Two sql select queries into one

I have one select query like this, in a table where only the message id, author and recipient are stored: (I want to get all the message_id's)

SELECT message_id FROM messages_to WHERE author_id=0

With this list of message_id's, I want to SELECT in another table to get the actual messages:

SELECT messages FROM messages WHERE message_id=(message_id_from_before)

Both queries can return multiple results.

Is it possible to have one single query which returns the result of the second SELECT query? Unfortunately I don't have the requirement knowledge to do that; if someone could give me a small tip to do this I would be incredibly grateful.

Upvotes: 0

Views: 44

Answers (2)

Meghendra S Yadav
Meghendra S Yadav

Reputation: 134

You can use join to get the result from both the table, as you mentioned message_id in both the table.

Try this

SELECT messages.message FROM messages 
Inner Join message_id on message_id.message_id = messages.message_id
WHERE message_id.author_id = 0

Upvotes: 0

flwd
flwd

Reputation: 613

You can use a subquery in your condition:

SELECT messages FROM messages WHERE message_id IN (
  SELECT message_id FROM messages_to WHERE autor_id=0
)

Upvotes: 2

Related Questions