MySQL 即使没有结果也返回一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12449899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:52:20  来源:igfitidea点击:

Returning a value even if no result

mysql

提问by Maxime Laval

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

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

SELECT field1 FROM table WHERE id = 123 LIMIT 1;

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.

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

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

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

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

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

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

Thanks!

谢谢!

Edit following some comments and answers: yes it has to be in a single query statementand 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()).

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

回答by K.A.F.

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

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

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

回答by Mark N Hopgood

As you are looking for 1 record, (LIMIT 1) then this will work.

当您正在寻找 1 条记录时,(LIMIT 1) 那么这将起作用。

(SELECT field1 FROM table WHERE id = 123) 
UNION 
(SELECT 'default_value_if_no_record')
LIMIT 1;

Can be a handy way to display default values, or indicate no results found. I use it for reports.

可以方便地显示默认值,或指示未找到结果。我用它来做报告。

See also http://blogs.uoregon.edu/developments/2011/03/31/add-a-header-row-to-mysql-query-results/for a way to use this to create headers in reports.

另请参阅http://blogs.uoregon.edu/developments/2011/03/31/add-a-header-row-to-mysql-query-results/以了解使用它在报告中创建标题的方法。

回答by Andreas Wederbrand

You could include count(id). That will always return.

你可以包括count(id)。那总会回来的。

select count(field1), field1 from table where id = 123 limit 1;

http://sqlfiddle.com/#!2/64c76/4

http://sqlfiddle.com/#!2/64c76/4

回答by Nikunj K.

You can use COALESCE

您可以使用COALESCE

SELECT COALESCE(SUM(column),0)
FROM   table

回答by Suzan Cioc

Do search with LEFT OUTER JOIN. I don't know if MySQL allows inline VALUESin join clauses but you can have predefined table for this purposes.

使用 进行搜索LEFT OUTER JOIN。我不知道 MySQL 是否允许VALUES在 join 子句中内联,但您可以为此目的预定义表。

回答by Kai Liu

k-a-f's answer works for selecting one column, if selecting multiple column, we can.

kaf 的答案适用于选择一列,如果选择多列,我们可以。

DECLARE a BIGINT DEFAULT 1;
DECLARE b BIGINT DEFAULT "name";

SELECT id, name from table into a,b;

Then we just need to check a,b for values.

然后我们只需要检查 a,b 的值。