0

ok, my question today is that: i want to get data from table according to criteria. if no result i want it to use a given value. my query looks like that :

select  distinct COALESCE(table.Name,'a') as name, 
'b' as val1, 
'c' as val2,  
'd' as val3 
from  table 
where table.Id='255';

If there is Id 255 I want the query to return the name with that id. otherwise, I want it to return the value 'a'. other fields should return their values anyway. when I use the above query I get an empty result set because there is no Id = '255' in table.

anyone? thanks a lot

Prashant Ghimire
  • 4,890
  • 3
  • 35
  • 46
  • You want to return 'a' if the result set is empty (no row with that id) and the value of 'name' if there is a result, right? Youre not looking for IFNULL I guess – Merlin Denker Mar 05 '14 at 10:45
  • yep, you right. ifnull was my first guess. i used the COALESCE function because someone said its better then ifnull function. anyway it does the same job, but not the job i want.. – Yosi Nesimyan Mar 05 '14 at 10:47
  • possible duplicate of [Returning a value even if no result](http://stackoverflow.com/questions/12449899/returning-a-value-even-if-no-result) – Ragen Dazs Mar 05 '14 at 10:51
  • Has been asked and answered before. Unfortunately I dont know how to mark this post as a duplicate... So I'm just gonna give you the link: https://stackoverflow.com/questions/12449899/returning-a-value-even-if-no-result EDIT: Found out and flagged it... should be fixed soon. – Merlin Denker Mar 05 '14 at 10:41

0 Answers0