2015-07-21 61 views
0

我有一個選擇,我想通過左連接搜索一個表的列只有當用戶啓用了前置條件來搜索搜索作爲添加表的條件我有點困惑,我不知道如果我單獨搜索或我立刻做的一切.... 他說,這是我看的MySQL系統,但我看到這麼如果選擇添加代碼塊

if .condition... then ... end if; 

我的代碼

select user. * from 
u user 
if (u.visible == 1, 
    left join houseUser hu on u.id = h.id_user 
    left join h house on hu.id_house = h.id_house 
) 
where 
u.age> 30 

如果可見,添加該代碼可以看到用戶在哪個房屋。

回答

0

你可以只納入條件爲on條款:

select u.*, hu.*, h.* 
from user u left join 
    houseUser hu 
    on u.id = h.id_user and u.visible = 1 left join 
    h house 
    on hu.id_house = h.id_house and u.visible = 1 
where u.age > 30; 

列會回來,但他們會NULLvisible1