1. <tfoot id='OxPmw'></tfoot>
        <i id='OxPmw'><tr id='OxPmw'><dt id='OxPmw'><q id='OxPmw'><span id='OxPmw'><b id='OxPmw'><form id='OxPmw'><ins id='OxPmw'></ins><ul id='OxPmw'></ul><sub id='OxPmw'></sub></form><legend id='OxPmw'></legend><bdo id='OxPmw'><pre id='OxPmw'><center id='OxPmw'></center></pre></bdo></b><th id='OxPmw'></th></span></q></dt></tr></i><div id='OxPmw'><tfoot id='OxPmw'></tfoot><dl id='OxPmw'><fieldset id='OxPmw'></fieldset></dl></div>
      1. <legend id='OxPmw'><style id='OxPmw'><dir id='OxPmw'><q id='OxPmw'></q></dir></style></legend>

        <small id='OxPmw'></small><noframes id='OxPmw'>

        • <bdo id='OxPmw'></bdo><ul id='OxPmw'></ul>

      2. 如何在 MySQL 中完全 OUTER JOIN 多个表

        时间:2023-10-09
        • <bdo id='YEFzd'></bdo><ul id='YEFzd'></ul>

        • <tfoot id='YEFzd'></tfoot>
            <tbody id='YEFzd'></tbody>
          • <legend id='YEFzd'><style id='YEFzd'><dir id='YEFzd'><q id='YEFzd'></q></dir></style></legend>
          • <i id='YEFzd'><tr id='YEFzd'><dt id='YEFzd'><q id='YEFzd'><span id='YEFzd'><b id='YEFzd'><form id='YEFzd'><ins id='YEFzd'></ins><ul id='YEFzd'></ul><sub id='YEFzd'></sub></form><legend id='YEFzd'></legend><bdo id='YEFzd'><pre id='YEFzd'><center id='YEFzd'></center></pre></bdo></b><th id='YEFzd'></th></span></q></dt></tr></i><div id='YEFzd'><tfoot id='YEFzd'></tfoot><dl id='YEFzd'><fieldset id='YEFzd'></fieldset></dl></div>

            <small id='YEFzd'></small><noframes id='YEFzd'>

                  本文介绍了如何在 MySQL 中完全 OUTER JOIN 多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要FULL OUTER JOIN多个表.我知道如何从这里FULL OUTER JOIN两个表.但是我有几张表,我无法将其应用于它们.我怎样才能实现它?
                  我的 SQL 代码如下:

                  I need to FULL OUTER JOIN multiple tables. I know how to FULL OUTER JOIN two tables from here. But I have several tables, and I can't apply it over them. How can I achieve it?
                  My SQL code, below:

                  INSERT INTO table
                  (
                    customer_id
                   ,g01
                   ,g02
                   ,g03
                   ,has_card
                   ,activity
                    )
                    SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
                    FROM s_geo_data sgd
                    LEFT JOIN s_category sc
                    ON sc.customer_id = sgd.customer_id
                      UNION
                    SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
                    FROM s_geo_data sgd
                    RIGHT JOIN s_category sc
                    ON sc.customer_id = sgd.customer_id
                  
                      UNION
                  
                    SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
                    FROM s_geo_data sgd
                    LEFT JOIN s_activity a
                    ON a.customer_id = sgd.customer_id
                      UNION
                    SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
                    FROM s_geo_data sgd
                    RIGHT JOIN s_activity a
                    ON a.customer_id = sgd.customer_id
                  

                  我也试过这个查询:

                  INSERT INTO reportls.table
                  (
                    customer_id
                   ,g01
                   ,g02
                   ,g03
                   ,has_card
                   ,activity
                    )
                    SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
                    FROM s_geo_data sgd
                    LEFT JOIN s_category sc
                    ON sc.customer_id = sgd.customer_id
                    LEFT JOIN s_activity a
                    ON sc.customer_id = sgd.customer_id
                  
                      UNION
                  
                    SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
                    FROM s_geo_data sgd
                    LEFT JOIN s_category sc
                    ON sc.customer_id = sgd.customer_id
                    RIGHT JOIN s_activity a
                    ON a.customer_id = sgd.customer_id
                  
                      UNION
                  
                    SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity
                    FROM s_geo_data sgd
                    RIGHT JOIN s_category sc
                    ON sc.customer_id = sgd.customer_id
                    LEFT JOIN s_activity a
                    ON a.customer_id = sgd.customer_id
                  

                  最后一个查询执行很长时间,我需要更快的查询.

                  Last query executes very long time, I need faster query.

                  推荐答案

                  我认为有一个 FULL OUTER JOIN 超过 3 个表,你需要这样做:

                  I think to have a FULL OUTER JOIN over 3 tables, you need to do it like this:

                  SELECT t1.value, t2.value, t3.value
                  FROM t1 LEFT JOIN t2 ON t1.value = t2.value
                          LEFT JOIN t3 ON t1.value = t3.value
                  UNION ALL
                  SELECT t1.value, t2.value, t3.value
                  FROM t2 LEFT JOIN t1 ON t1.value = t2.value
                          LEFT JOIN t3 ON t2.value = t3.value
                  WHERE t1.value IS NULL
                  UNION ALL
                  SELECT t1.value, t2.value, t3.value
                  FROM t3 LEFT JOIN t1 ON t1.value = t3.value
                          LEFT JOIN t2 ON t2.value = t3.value
                  WHERE t1.value IS NULL AND t2.value IS NULL
                  

                  作为替代方案:

                  SELECT t1.value, t2.value, t3.value
                  FROM t1 FULL OUTER JOIN t2 ON t1.value = t2.value
                          FULL OUTER JOIN t3 ON t1.value = t3.value
                  

                  <小时>

                  我建议你创建一些临时表,比如 t1t2t3 来存储你的查询结果,然后使用上面的查询


                  I suggest you to create some temporary tables like t1, t2 and t3 for storing results of your queries, then use above query over those.

                  这篇关于如何在 MySQL 中完全 OUTER JOIN 多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MYSQL 连接逗号分隔查询 下一篇:SQL 查找重复条目(组内)

                  相关文章

                • <legend id='nUeze'><style id='nUeze'><dir id='nUeze'><q id='nUeze'></q></dir></style></legend>

                  • <bdo id='nUeze'></bdo><ul id='nUeze'></ul>
                      <tfoot id='nUeze'></tfoot>

                      <small id='nUeze'></small><noframes id='nUeze'>

                      <i id='nUeze'><tr id='nUeze'><dt id='nUeze'><q id='nUeze'><span id='nUeze'><b id='nUeze'><form id='nUeze'><ins id='nUeze'></ins><ul id='nUeze'></ul><sub id='nUeze'></sub></form><legend id='nUeze'></legend><bdo id='nUeze'><pre id='nUeze'><center id='nUeze'></center></pre></bdo></b><th id='nUeze'></th></span></q></dt></tr></i><div id='nUeze'><tfoot id='nUeze'></tfoot><dl id='nUeze'><fieldset id='nUeze'></fieldset></dl></div>