mysql - Returning a default value in SQL when no rows found -
how can change sql return default value if no rows found?
eg:
select text,name text_file text='met'
if no rows found, text contain 'values not found' example
one method uses union all
:
select text, name text_file text = 'met' union select max('values not found'), null text_file text = 'met' having count(*) = 0;
notes. second subquery aggregation query. returns 1 row (without having
). row has looking for.
second, type of operation should done in application , not database.
third, if expecting 1 row, can use aggregation query such as:
select (case when count(*) > 0 text else 'values not found' end) text, (case when count(*) > 0 name end) name text_file text = 'met'
Comments
Post a Comment