简体   繁体   English

两种方式加入mysql查询

[英]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 我想要访问所有带有标签happy的消息,如何以最佳方式构造查询:))谢谢

ps this is just an example database ps这只是一个示例数据库

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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