0

I am using cakephp 3 to replace with a static word in place of null/empty column value.

For Example :- I have a product table which contain id,price,image, I need to fetch all the records but which product's does not have image, it should return "no_image.jpg" in place of null/empty.

Like : SELECT if(image='','no_image.jpg', image), id, price FROM user_images. how can i convert above query in cakephp 3. Thanks in Advance.

sandy
  • 3
  • 1
  • 5

1 Answers1

0

You can use NULLIF to set NULL when image = ''.

Then use COALESCE to return the second parameter if first is null.

SELECT COALESCE(NULLIF(image,''), 'no_image.jpg') as image, id, price FROM user_images

In other words, if image is NULL or '', it will return no_image.jpg,, else it will return the image itself.

Of course, you can also return null/empty and put this logic in application, not in the query.

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29