Selecting default value query returns blank

Posted on

Question :

I am trying to select the default value if a row do not have a value with this snippet

1

SELECT IFNULL((SELECT as_title FROM all_scheduled_messages WHERE id = 1 LIMIT 1) ,'lorem ipsum');

When i run the query,it returns a blank and so is when i

2

SELECT as_title FROM all_scheduled_messages WHERE id = 1 LIMIT 1

Why is my first query returning the text i want as default?.

Answer :

I think you’re going about this all wrong.

You want to wrap IFNULL around the actual field.

SELECT IFNULL(as_title, 'lorem ipsum') FROM all_scheduled_messages WHERE id = 1 LIMIT 1;

However, if you’re returning a blank, rather than a NULL from your query, IFNULL will not work. You’ll need to NULL the value first:

SELECT IFNULL(NULLIF(as_title, ''), 'lorem ipsum') FROM all_scheduled_messages WHERE id = 1 LIMIT 1;

Leave a Reply

Your email address will not be published.