我有以下两张桌子:

Codes
Code  key1   key2
1     a      hh
2     a
3     a
4     a


Orders
key1   key2  val1
a      aa    foo
h      hh    bar

我想得到
Desired output
code   key1   key2  val1
1      a      hh    bar
2      a
3      a
4      a

我试过用内部连接命令:
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

但这会去掉key2列中的所有空数据,我得到:
code   key1   key2   val1
1      a      hh     bar

对我如何做到这点有什么建议吗?

最佳答案

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

关于mysql - MySQL多表联接为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54471533/

10-09 20:51