Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a value even if no result

Tags:

mysql

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;

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.

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);

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

Thanks!

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()).

like image 525
Maxime Laval Avatar asked Sep 16 '12 19:09

Maxime Laval


People also ask

What does a select statement return if not found?

It returns either field1 if the row exists, otherwise 0.

How do I make a count 0 return in SQL?

An ugly workaround, if you want your original query to return a row with 0's, when no records are present, is to add something like this to your query: UNION SELECT NULL AS [Month], 0 AS [COUNT], 0 AS [GRAMS], 0 AS [PRINCIPAL] WHERE (SELECT COUNT(*) FROM #AllExpired) = 0 , but a better solution would be to have your ...

Can a SQL query return null?

The IS NULL condition is used in SQL to test for a NULL value. It returns TRUE if a NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How do I assign a default value if no rows returned from the select query MySQL?

Here's one way: SELECT *, IFNULL( ( SELECT col1 FROM table1 WHERE col1 IN ('012311','0123631','091233','092111') ), 'some_value' ) AS my_col1 FROM table1; Not neccessarily copy+paste, you will have to adjust for your specific case.


3 Answers

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');
like image 107
K.A.F. Avatar answered Oct 17 '22 14:10

K.A.F.


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

(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.

like image 39
Mark N Hopgood Avatar answered Oct 17 '22 12:10

Mark N Hopgood


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

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

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

like image 21
Andreas Wederbrand Avatar answered Oct 17 '22 12:10

Andreas Wederbrand