getaway
getaway

Reputation: 8990

two way joins mysql query

message table

id    user_id| message
 1       1   | this is my cruel message
 2       1   | this is my happy message
 3       2   | this is happy messgae    

message_tags table

id    message_id| tags
 1       2      | happy
 2       3      | happy

what i want to acess all the messages that have the the tag happy, how would construct the query in the best possible way :)) thanks

p.s. this is just an example database

Upvotes: 0

Views: 362

Answers (1)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171411

select m.id, m.user_id, m.message,
    u.Username 
from message m
inner join user_table u on m.user_id = u.id
where m.id in (select message_id from message_tags where tags = 'happy')

Upvotes: 2

Related Questions