mysql - 在 MySQL 中加入查询结果
问题描述
我在 MySQL 数据库中工作。
我有三张桌子。1 , 2 & 3
我想在 id 上加入表 2 和表 3 到表 1 的结果。我想保留表 1 的所有条目,并在调用 id 上加入 2 和 3 的结果之后,并且它不匹配的地方有一个空值。
Table 1 has callid
Table 2 has callid and invoiceid
Table 3 has invoiceid and customerid
因此,在 invoiceid 上加入表 2 和 3 并按 customerid = xyz 过滤,然后将结果加入到 callid 上的表 1。表 1 还将有一个 Where 子句过滤日期
结果看起来像这样
callid customerid
123 xyz
124 xyz
125 null
126 xyz
提前谢谢你
解决方案
我想你在描述left join
:
select . . .
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid;
您可以在on
子句中过滤客户 ID:
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid and t3.customerid = ?;
但是,这可能不是您想要的。您可能想要过滤内部连接,然后执行以下操作left join
:
select . . .
from table1 t1 left join
(table2 t2 join
table3 t3
on t3.invoiceid = t2.invoiceid
)
on t1.callid = t2.callid and
t3.customerid = ?
推荐阅读
- swift - 由于“内部”保护级别,“removeChild”不可访问
- expect - 从远程服务器获取文件然后删除
- r - 将字符转换为分钟数
- android - 向下滚动时从多个来源附加到适配器列表
- c++ - 结构/类包装器中的 C++ 重载自动运算符
- c# - 如何将 MassTransit 演示分成生产者和消费者应用程序
- django - 在 Django 中,预选表单选项标签的正确方法是什么?
- python - 如果将大量使用计算,我应该将计算存储在变量中吗?
- linux - 在 CentOS 中配置 Nginx 以加载 Laravel 5.8 站点
- javascript - 如何使用 Sinon 存根“包装器”函数?