Page 1 of 1

Select subject if not empty, otherwise select text

Posted: Tue Dec 28, 2010 2:20 am
by Luke
I am trying to write a query that selects the "subject" column unless that column is empty, in which case it should select the text "(No Subject)". Is there a function or something I can use in MySQL to accomplish something like this?

Code: Select all

SELECT
    UNLESS_IS_EMPTY(subject, '(No Subject)') -- Is there a function that does something like this?
FROM messages
WHERE
   ...

Re: Select subject if not empty, otherwise select text

Posted: Tue Dec 28, 2010 2:24 am
by Luke
Heh, nevermind! I found the function I need. It's called "IF":

Code: Select all

SELECT
    IF(subject = '', '(No Subject)', subject) AS title
FROM messages
WHERE
   ...