衫裤跑路 2011-01-25 17:34 采纳率: 50%
浏览 540
已采纳

如何在 MySQL 中实现完全外部连接?

I want to do a Full Outer Join in MySQL. Is this possible? Is a Full Outer Join supported by MySQL?

转载于:https://stackoverflow.com/questions/4796872/how-to-do-a-full-outer-join-in-mysql

  • 写回答

15条回答 默认 最新

  • 笑故挽风 2011-01-25 17:38
    关注

    You don't have FULL JOINS on MySQL, but you can sure emulate them.

    For a code SAMPLE transcribed from this SO question you have:

    with two tables t1, t2:

    SELECT * FROM t1
    LEFT JOIN t2 ON t1.id = t2.id
    UNION
    SELECT * FROM t1
    RIGHT JOIN t2 ON t1.id = t2.id
    

    The query above works for special cases where a FULL OUTER JOIN operation would not produce any duplicate rows. The query above depends on the UNION set operator to remove duplicate rows introduced by the query pattern. We can avoid introducing duplicate rows by using an anti-join pattern for the second query, and then use a UNION ALL set operator to combine the two sets. In the more general case, where a FULL OUTER JOIN would return duplicate rows, we can do this:

      SELECT * FROM t1
      LEFT JOIN t2 ON t1.id = t2.id
      UNION ALL
      SELECT * FROM t1
      RIGHT JOIN t2 ON t1.id = t2.id
      WHERE t1.id IS NULL
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(14条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器