<tfoot id='O1fsm'></tfoot>
    <legend id='O1fsm'><style id='O1fsm'><dir id='O1fsm'><q id='O1fsm'></q></dir></style></legend>

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

      1. <small id='O1fsm'></small><noframes id='O1fsm'>

      2. 如何在 SQL SELECT 中执行 IF...THEN?

        时间:2023-07-18
          <bdo id='oxiJD'></bdo><ul id='oxiJD'></ul>
          <tfoot id='oxiJD'></tfoot>
        • <legend id='oxiJD'><style id='oxiJD'><dir id='oxiJD'><q id='oxiJD'></q></dir></style></legend>
              <i id='oxiJD'><tr id='oxiJD'><dt id='oxiJD'><q id='oxiJD'><span id='oxiJD'><b id='oxiJD'><form id='oxiJD'><ins id='oxiJD'></ins><ul id='oxiJD'></ul><sub id='oxiJD'></sub></form><legend id='oxiJD'></legend><bdo id='oxiJD'><pre id='oxiJD'><center id='oxiJD'></center></pre></bdo></b><th id='oxiJD'></th></span></q></dt></tr></i><div id='oxiJD'><tfoot id='oxiJD'></tfoot><dl id='oxiJD'><fieldset id='oxiJD'></fieldset></dl></div>

                    <tbody id='oxiJD'></tbody>
                • <small id='oxiJD'></small><noframes id='oxiJD'>

                  本文介绍了如何在 SQL SELECT 中执行 IF...THEN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在 SQL SELECT 语句中执行 IF...THEN?

                  How do I perform an IF...THEN in an SQL SELECT statement?

                  例如:

                  SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product
                  

                  推荐答案

                  CASE 语句是 SQL 中最接近 IF 的语句,所有版本的 SQL Server 都支持.

                  The CASE statement is the closest to IF in SQL and is supported on all versions of SQL Server.

                  SELECT CAST(
                               CASE
                                    WHEN Obsolete = 'N' or InStock = 'Y'
                                       THEN 1
                                    ELSE 0
                               END AS bit) as Saleable, *
                  FROM Product
                  

                  如果您希望结果为布尔值,则只需使用 CAST 运算符.如果您对 int 感到满意,则此方法有效:

                  You only need to use the CAST operator if you want the result as a Boolean value. If you are happy with an int, this works:

                  SELECT CASE
                              WHEN Obsolete = 'N' or InStock = 'Y'
                                 THEN 1
                                 ELSE 0
                         END as Saleable, *
                  FROM Product
                  

                  CASE 语句可以嵌入到其他 CASE 语句中,甚至可以包含在聚合中.

                  CASE statements can be embedded in other CASE statements and even included in aggregates.

                  SQL Server Denali (SQL Server 2012) 添加了 IIF 声明,也可在 access (马丁·史密斯指出):

                  SQL Server Denali (SQL Server 2012) adds the IIF statement which is also available in access (pointed out by Martin Smith):

                  SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Saleable, * FROM Product
                  

                  这篇关于如何在 SQL SELECT 中执行 IF...THEN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQLSERVER 中的 ListAGG 下一篇:在 SQL Server 中删除日期时间的时间部分的最佳方法

                  相关文章

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

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

                  2. <legend id='kwLex'><style id='kwLex'><dir id='kwLex'><q id='kwLex'></q></dir></style></legend>