mysql - MySQL multi-table join with nulls
问题描述
I have the following two tables:
Codes
Code key1 key2
1 a hh
2 a
3 a
4 a
Orders
key1 key2 val1
a aa foo
h hh bar
And I'd like to get
Desired output
code key1 key2 val1
1 a hh bar
2 a
3 a
4 a
I've tried it with the inner join on orders:
select
ordersa.key2,
code,
ordersb.date
from
order ordersa
right join codes
on codes.key1 = orders.key1
inner join orders ordersb
on codes.key2 = ordersb.key2
But that takes out all the null data in key2 column and I get:
code key1 key2 val1
1 a hh bar
Any suggestions on how I can achieve that?
解决方案
SELECT
c.Code,
c.key1,
IFNULL(c.key2, '') key2,
IFNULL(o.val1, '') val1,
FROM Codes c
LEFT JOIN Orders o ON c.key2 = o.key2
推荐阅读
- javascript - 各种 globalCompositeOperation 模式的 WebGL 等效项是什么?
- javascript - 单击按钮时难以更改谷歌地图的样式
- go - 从插件中删除主机的源代码以减小文件大小
- c# - EnumWindows 找不到触摸键盘
- r - R 的 assign() 函数何时合适?
- javafx - TornadoFX 中 FXML 文件的前缀路径
- java - Inheriting specifid methods in java
- javascript - Javascript正文溢出:隐藏
- javascript - Express js中的firebase googlesignin错误
- symfony - 指向路由的 Twig_Environment 链接不起作用