Page 1 of 1

if not null then select in a mysql statement

Posted: Fri Mar 06, 2009 5:45 am
by chidge
hi

I am wondering if there is a better way to do this?


Code: Select all

SELECT  
(SELECT title from jnl_article WHERE blog_id = jnl_id) AS title,
DATE_FORMAT(date, '%D of %M %Y at %l:%i %p') AS datetime,
IF(folder IS NULL, 'none', folder) AS folder,
IF(folder IS NULL, 'none', (SELECT filename FROM jnl_main_image WHERE blog_id = jnl_id)) AS filename,
IF(folder IS NULL, 'none', (SELECT caption FROM jnl_main_image WHERE blog_id = jnl_id)) AS caption  
FROM jnl
WHERE status = 2
ORDER BY date DESC LIMIT 15
basically i get the title from another table in a subquery and then if there is a value in the folder option in the jnl table the statement will then go on to get the filename and the caption from jnl_main_image

What I am wondering is if there is a better way to do this? Can I do an

if folder then do this statement?

I am a little usureas to how to go about this and have a good look over the mysql docs - looks like a case statement or an if but ifs seem to just be able to give one value back.....

sorry if its a silly noobie question

and thanks in advance