Using if statement in a select and a possible subselect?

Hi,

I’m trying to create a custom language script in php and mysql. Basically how I have it now, Is I do two queries… one I try selecting the language in the database. To see if any record for that page exists in the second language. If true the second sql statment will pull on that language id, if not, then it will pull on the default language id.

So I’m using this table:

PAGES
id | page_id | language_id | page_meta_title | page_title | page_copy

Can I do a single query, ie, select distinct page_id value, where page_id = “current page id” & language_id = “current selected language”

if no value

select * from PAGES where page_id = “current page id” and language_id = 1

else

select * from PAGES where page_id = “current page id” and language_id = “current selected language”

something like: (untested)

Where: German is desired and English is the default (known to exist) - replace languages_id with proper surrogate key


SELECT
     IF(t.pages_id IS NULL,d.page_meta_title,t.page_meta_title) page_meta_title
     ,IF(t.pages_id IS NULL,d.page_title,t.page_title) page_title
     ,IF(t.pages_id IS NULL,d.page_copy,t.page_copy) page_copy
  FROM
     pages d
  LEFT OUTER
  JOIN
     pages t
    ON
     d.pages_id = t.pages_id
   AND
     t.languges_id = 'german'
 WHERE
     d.pages_id = 89
   AND
     d.languages_id = 'english'