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
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