简体   繁体   中英

How do i use CASE in mysql select query with left join?

select  r.email,
        rc.cuisine_id,
        rc.restaurant_id, 
        c.id as c_id ,
        c.name as cus_name,
        r.image,
        r.web_address,
        r.area,
        rp.TypeId,  
        rp.PaymentStatus , 
        rp.RestaurantId 
from restaurant as r
left join restaurant_cuisine as rc on r.id=rc.restaurant_id
left join cuisine as c on rc.cuisine_id=c.id  where c.status='1'

Case statement that is needed

CASE WHEN rp.TypeId = '1' 
     THEN left join restaurant_payment as rp on r.id=rp.RestaurantId 
     WHEN rp.TypeId = '3'
     THEN left join users as u on rp.RestaurantId=u.id
END

Try this

select  r.email,
        rc.cuisine_id,
        rc.restaurant_id, 
        c.id as c_id ,
        c.name as cus_name,
        r.image,
        r.web_address,
        r.area,
        rp.TypeId,  
        rp.PaymentStatus , 
        rp.RestaurantId 
from restaurant as r
left join restaurant_cuisine as rc on r.id=rc.restaurant_id
left join cuisine as c on rc.cuisine_id=c.id  where c.status='1' and
rp.TypeId = CASE 
                 WHEN rp.TypeId = '1' 
                 THEN left join restaurant_payment as rp on r.id=rp.RestaurantId 
                 WHEN rp.TypeId = '3'
                 THEN left join users as u on rp.RestaurantId=u.id
            END

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