mysql - 如果添加 block 代码,请在其中选择

标签 mysql sql database

我有一个选择,并且仅当用户启用了 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/

相关文章:

mysql - SQL:查询多级分层表

mysql - 将两个查询的结果合并为一个

java - SQL 中的动态绑定(bind)

sql - 更新现有表时出现空约束错误

sql - 引用匿名 block 中的游标

java - MySQL 和 Java : Byte array not same when retrieved as it was when stored

mysql - 将 3 个不同的 mysql 查询合并为一个

android - 通过排序从 dynamoDB 中获取项目

database - Scriptella - 使用输出中的行填充新表的数据库到数据库 ETL 脚本

mysql - 查询 table.column 不在替换另一个表的更改列中的位置