mysql - SQL: Default where clause -


i'm creating multi. language website. here sample sql

select *  admin_real_estate_category_languages category_languages left join admin_real_estate_categories category on category_languages.real_estate_category_id = category.id  language = 'jp' , category = 'area' 

all details have language = 'en' not have language = 'jp', if language = 'jp' not exist in details want query language = 'en'.

in short want query have default clause if first clause not return anything.

here sample of goal.

select *  admin_real_estate_category_languages category_languages left join admin_real_estate_categories category on category_languages.real_estate_category_id = category.id  language = 'jp' , category = 'area'  default language = 'en' , category = 'area' 

thanks :)

while might not best solution since introduces additional join, can solve problem in way.

first off, don't want select *, that's bad idea. select columns want use instead (even if columns). if have actual columns can use coalesce solve problem.

since haven't provided actual schema i'm guessing bit here, you'll gist of solution. notice i've switched places on language table , category table join language table on category table, not other way around doing in main question.

select category.foo,         category.bar,         category.baz,         coalesce(category_language.text, default_category_language.text) admin_real_estate_categories category  left join admin_real_estate_category_languages category_language      on category_language.real_estate_category_id = category.id      , category_language.language = 'jp' join admin_real_estate_category_languages default_category_language      on default_category_language .real_estate_category_id = category.id     , default_category_language.language = 'en' category.category = 'area' 

of course, can escalate , out of hand if need many columns type of handling. if need type of handling on 1 or 2 columns strategy works fine, more , shouldn't use strategy. in case i'd split query instead (just make sure not accidentially create sort of select n+1 monstrosity if that!).


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -