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

        <tfoot id='qs4Gr'></tfoot>

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

        MySQL中的Case语句

        时间:2023-06-02
        <tfoot id='o4ZmP'></tfoot>

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

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

                <tbody id='o4ZmP'></tbody>

                • <bdo id='o4ZmP'></bdo><ul id='o4ZmP'></ul>
                  <legend id='o4ZmP'><style id='o4ZmP'><dir id='o4ZmP'><q id='o4ZmP'></q></dir></style></legend>
                  本文介绍了MySQL中的Case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个名为tbl_transaction"的数据库表,其定义如下:

                  I have a database table called 'tbl_transaction' with the following definition:

                  id INT(11) Primary Key
                  action_type ENUM('Expense', 'Income')
                  action_heading VARCHAR (255)
                  action_amount FLOAT
                  

                  我想生成两列:Income AmtExpense Amt.

                  I would like to generate two columns: Income Amt and Expense Amt.

                  是否可以仅使用 SQL 查询有条件地填充列,以便输出显示在正确的列中,具体取决于它是费用项目还是收入项目?

                  Is it possible to populate the columns conditionally, using only a SQL Query, such that the output appears in the correct column, depending on whether it is an Expense item or an Income item?

                  例如:

                  ID        Heading         Income Amt       Expense Amt
                  1         ABC             1000             -
                  2         XYZ             -                2000
                  

                  我使用 MySQL 作为数据库.我正在尝试使用 CASE 语句来完成此操作.

                  I'm using MySQL as the database. I'm trying to use the CASE statement to accomplish this.

                  干杯!

                  推荐答案

                  是的,像这样:

                  SELECT
                      id,
                      action_heading,
                      CASE
                          WHEN action_type = 'Income' THEN action_amount
                          ELSE NULL
                      END AS income_amt,
                      CASE
                          WHEN action_type = 'Expense' THEN action_amount
                          ELSE NULL
                      END AS expense_amt
                  
                  FROM tbl_transaction;
                  

                  <小时>

                  正如其他答案所指出的那样,MySQL 还具有 IF() 函数来使用较少冗长的语法来执行此操作.我通常会尽量避免这种情况,因为它是 SQL 的特定于 MySQL 的扩展,其他地方通常不支持.CASE 是标准 SQL,并且在不同的数据库引擎之间具有更高的可移植性,我更喜欢尽可能编写可移植的查询,仅在可移植的替代方案相当时才使用特定于引擎的扩展em> 较慢或不太方便.


                  As other answers have pointed out, MySQL also has the IF() function to do this using less verbose syntax. I generally try to avoid this because it is a MySQL-specific extension to SQL that isn't generally supported elsewhere. CASE is standard SQL and is much more portable across different database engines, and I prefer to write portable queries as much as possible, only using engine-specific extensions when the portable alternative is considerably slower or less convenient.

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

                  上一篇:在 MySQL 中加载数据 infile 的访问被拒绝 下一篇:在 MySQL 中查找下一个可用的 id

                  相关文章

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

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

                    <tfoot id='xuNkY'></tfoot>
                  1. <legend id='xuNkY'><style id='xuNkY'><dir id='xuNkY'><q id='xuNkY'></q></dir></style></legend>
                      • <bdo id='xuNkY'></bdo><ul id='xuNkY'></ul>