Click here to Skip to main content
15,909,576 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Please i dont now how to return default values when no rows returned,

i tried COALESCE((),()) but coalesce should contain 1 column

for example i want to return manu column like : (select '12-12-12',312,1)

What I have tried:

SQL
SELECT COALESCE( (select CIN from Pers where Id=32323) ,(select '12-12-12',312,'zfsdf'))
Posted
Updated 4-Feb-18 9:24am
Comments
Rajesh Pandya 24-Jan-18 8:25am    
Store the coalesce function value in variable and check if value is blank or null then assign the default value in the variable.

1 solution

You can use IF ... ELSE ... END IF
See: MySQL :: MySQL 5.7 Reference Manual :: 13.6.5.2 IF Syntax[^]

Another way is to use UNION + LIMIT:
SQL
(select Col1, Col2, Col3 FROM Pers WHERE Id=32323)
UNION
(SELECT '12-12-12' AS Col1, 312 AS Col2, 'zfsdf' AS Col3)
LIMIT 1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900