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

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

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

        <bdo id='FK8r2'></bdo><ul id='FK8r2'></ul>
      1. 条件 JOIN 语句 SQL Server

        时间:2023-10-08

          <tfoot id='r2yZq'></tfoot>

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

              <bdo id='r2yZq'></bdo><ul id='r2yZq'></ul>
                  <i id='r2yZq'><tr id='r2yZq'><dt id='r2yZq'><q id='r2yZq'><span id='r2yZq'><b id='r2yZq'><form id='r2yZq'><ins id='r2yZq'></ins><ul id='r2yZq'></ul><sub id='r2yZq'></sub></form><legend id='r2yZq'></legend><bdo id='r2yZq'><pre id='r2yZq'><center id='r2yZq'></center></pre></bdo></b><th id='r2yZq'></th></span></q></dt></tr></i><div id='r2yZq'><tfoot id='r2yZq'></tfoot><dl id='r2yZq'><fieldset id='r2yZq'></fieldset></dl></div>
                  <legend id='r2yZq'><style id='r2yZq'><dir id='r2yZq'><q id='r2yZq'></q></dir></style></legend>
                    <tbody id='r2yZq'></tbody>
                  本文介绍了条件 JOIN 语句 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以执行以下操作:

                  Is it possible to do the following:

                  IF [a] = 1234 THEN JOIN ON TableA 
                  ELSE JOIN ON TableB
                  

                  如果是,正确的语法是什么?

                  If so, what is the correct syntax?

                  推荐答案

                  我认为通过将 Initial 表加入 Option_AOption_B 使用 LEFT JOIN,这将产生如下结果:

                  I think what you are asking for will work by joining the Initial table to both Option_A and Option_B using LEFT JOIN, which will produce something like this:

                  Initial LEFT JOIN Option_A LEFT JOIN NULL
                  OR
                  Initial LEFT JOIN NULL LEFT JOIN Option_B
                  

                  示例代码:

                  SELECT i.*, COALESCE(a.id, b.id) as Option_Id, COALESCE(a.name, b.name) as Option_Name
                  FROM Initial_Table i
                  LEFT JOIN Option_A_Table a ON a.initial_id = i.id AND i.special_value = 1234
                  LEFT JOIN Option_B_Table b ON b.initial_id = i.id AND i.special_value <> 1234
                  

                  完成此操作后,您将忽略"NULL 集.这里的额外技巧是在 SELECT 行中,您需要在其中决定如何处理 NULL 字段.如果 Option_A 和 Option_B 表相似,那么您可以使用 COALESCE 函数返回第一个非空值(如示例所示).

                  Once you have done this, you 'ignore' the set of NULLS. The additional trick here is in the SELECT line, where you need to decide what to do with the NULL fields. If the Option_A and Option_B tables are similar, then you can use the COALESCE function to return the first NON NULL value (as per the example).

                  另一种选择是,您只需列出 Option_A 字段和 Option_B 字段,然后让使用 ResultSet 的任何内容来确定要使用的字段.

                  The other option is that you will simply have to list the Option_A fields and the Option_B fields, and let whatever is using the ResultSet to handle determining which fields to use.

                  这篇关于条件 JOIN 语句 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:T-SQL 子查询 Max(Date) 和 Joins 下一篇:如何在 MySQL 数据库中存储一对多关系?

                  相关文章

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

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

                      <legend id='NEnMK'><style id='NEnMK'><dir id='NEnMK'><q id='NEnMK'></q></dir></style></legend>
                        <bdo id='NEnMK'></bdo><ul id='NEnMK'></ul>