Click to See Complete Forum and Search --> : multiple mysql join/overwrite?


DssTrainer
02-04-2009, 02:31 PM
I am trying to form a mysql statement that will look at 2 tables, "Product" and "Product Description" and depending on the language id that I pass, pull the corresponding language UNLESS there isn't one for that language, then I want to default to the english one

So looking at my example tables, you see I have 3 products. All 3 have english descriptions. But only 2 have spanish descriptions:
http://www.unbannable.com/table_example.jpg


1. If I want to query all products where language_id = 1 I'd do:

select * from product p left join product_description pd on (p.product_id = pd.product_id) where pd.language_id = '1'


That will bring back 3 items matching the english language:
1 | x123 | Apple | This is an apple
2 | y123 | Banana | This is a banana
3 | z123 | Orange | This is an orange

2. If I want to query all products where language_id = 2 I'd do:

select * from product p left join product_description pd on (p.product_id = pd.product_id) where pd.language_id = '2'


That will bring back 2 items matching the spanish language:
1 | x123 | Manzana | Esta es un manzana
2 | y123 | Plátano | Esta es un Plátano


But instead of just bringing back 2 items, I want it to bring back any additional items that it doesn't have a language for, in english. So I'd like to see:

1 | x123 | Manzana | Esta es un manzana
2 | y123 | Plátano | Esta es un Plátano
3 | z123 | Orange | This is an orange

Is there a way I can do it with multiple joins in a single sql statment to bring back the above?

Thanks

paulnic
02-05-2009, 09:22 AM
But instead of just bringing back 2 items, I want it to bring back any additional items that it doesn't have a language for, in english. So I'd like to see:

1 | x123 | Manzana | Esta es un manzana
2 | y123 | Plátano | Esta es un Plátano
3 | z123 | Orange | This is an orange

Is there a way I can do it with multiple joins in a single sql statment to bring back the above?

Thanks

could you explain this bit again i'm not sure i understand what u want

DssTrainer
02-09-2009, 11:59 AM
Lets start with a fresh example:
http://www.unbannable.com/currencydemotable.jpg

If I am querying on data where language_id = 1 (english), I want to see ONLY the english language:
http://www.unbannable.com/currencydemores1.jpg

If I am querying on data where language_id = 2 (spanish), I want to see the spanish language for the records that exist in that language, otherwise, fallback to english:
http://www.unbannable.com/currencydemores2.jpg

As you can see in the currency_description table, there is no spanish entry for currency_id 3, so I want it to fallback to the english entry for that record.

Any ideas how I can do this? COALESCE? WHEN?