<bdo id='HnvHa'></bdo><ul id='HnvHa'></ul>

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

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

        <tfoot id='HnvHa'></tfoot>
      4. 即使没有结果也返回一个值

        时间:2023-06-26
        • <bdo id='sWAE5'></bdo><ul id='sWAE5'></ul>

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

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

              • <small id='sWAE5'></small><noframes id='sWAE5'>

                <tfoot id='sWAE5'></tfoot>
                    <tbody id='sWAE5'></tbody>
                  本文介绍了即使没有结果也返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这种简单的查询,它为给定的 id 返回一个非空的整数字段:

                  I have this kind of simple query that returns a not null integer field for a given id:

                  SELECT field1 FROM table WHERE id = 123 LIMIT 1;
                  

                  问题是如果找不到 id,则结果集为空.我需要查询总是返回一个值,即使没有结果.

                  The thing is if the id is not found, the resultset is empty. I need the query to always return a value, even if there is no result.

                  我有这个东西,但我不喜欢它,因为它运行了 2 次相同的子查询:

                  I have this thing working but I don't like it because it runs 2 times the same subquery:

                  SELECT IF(EXISTS(SELECT 1 FROM table WHERE id = 123) = 1, (SELECT field1 FROM table WHERE id = 123 LIMIT 1), 0);
                  

                  如果该行存在,则返回 field1,否则返回 0.有什么方法可以改进吗?

                  It returns either field1 if the row exists, otherwise 0. Any way to improve that?

                  谢谢!

                  根据一些评论和答案进行编辑:是的,它必须在单个查询语句中,我不能使用计数技巧,因为我需要返回只有 1 个值(仅供参考,我使用 Java/Spring 方法 SimpleJdbcTemplate.queryForLong() 运行查询).

                  Edit following some comments and answers: yes it has to be in a single query statement and I can not use the count trick because I need to return only 1 value (FYI I run the query with the Java/Spring method SimpleJdbcTemplate.queryForLong()).

                  推荐答案

                  MySQL 有一个函数可以在结果为空时返回一个值.您可以在整个查询中使用它:

                  MySQL has a function to return a value if the result is null. You can use it on a whole query:

                  SELECT IFNULL( (SELECT field1 FROM table WHERE id = 123 LIMIT 1) ,'not found');
                  

                  这篇关于即使没有结果也返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:薛定谔 MySQL 表:存在,但不存在 下一篇:你如何为 MySQL 和 Postgres 编写不区分大小写的查询?

                  相关文章

                  <tfoot id='aR1Fd'></tfoot>

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

                        <bdo id='aR1Fd'></bdo><ul id='aR1Fd'></ul>