我有一个选择,并且仅当用户启用了 essar 搜索的先决条件作为添加表的条件时,我才想通过左连接搜索表的列我有点困惑,我不知道是否我分开搜索或者一次做所有事情...... 他说是让我看看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
如果可见,请添加该代码以查看用户所在的房子。
最佳答案
您可以将条件合并到 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;
这些列将会返回,但当 visible
不是 1
时,它们将为 NULL
。
关于mysql - 如果添加 block 代码,请在其中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549259/