简体   繁体   中英

Join another table with condition

I want to join another table if a column in current table equals to a specific value. ( ENUM type )

for example, assume a status column which has 4 types of ( Public, OnlyMe, Friend, Group )

I want to join table groups only when status = Group

Something like this.

This is not mysql syntax.

SELECT * FROM posts WHERE ( status = Public OR status = OnlyMe OR status = Friend )
OR ( IF(status=Group) JOIN groups )

How can I do that in mysql?

Something like this (I made up several column names for your tables):

SELECT p.id, p.status, p.text, g.name
FROM posts p
    LEFT OUTER JOIN groups g
        ON (p.status = 'Group' AND p.groupId = g.id);

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