简体   繁体   中英

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.

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
)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM