Results 1 to 2 of 2
Oct 4, 2010, 16:26 #1
- Join Date
- Nov 2002
- Madison, WI USA
- 0 Post(s)
- 0 Thread(s)
using if statement in a select and a possible subselect?
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:
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
select * from PAGES where page_id = "current page id" and language_id = "current selected language"Atomic Coffee Media
Oct 4, 2010, 22:14 #2
- Join Date
- Jul 2006
- Augusta, Georgia, United States
- 16 Post(s)
- 3 Thread(s)
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'