2015-07-21 60 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