Jump to content

Recommended Posts

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:

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

Link to comment
https://forums.phpfreaks.com/topic/143822-multple-mysql-joinmerge-query-possible/
Share on other sites

Hrmmm, no idea what the 'proper' solution would be, or even if there is one, but the easy solution would be to just always pull English.

 

 

select p.*,pd.*, pd2.name as name_eng, pd2.description as desc_eng from product p left join product_description pd on (p.product_id = pd.product_id AND pd.language = '1') LEFT JOIN product_description pd2 ON (pd2.product_id = p.product_id AND pd2.language = '2');

nah.

 

The solution is a multipart join using mysql's COALESCE option to basically join the description table with one language to the description table of the other language and COALESCE will take the one that has a value.

 

But that single query takes more time, and is less flexible than a 2 simple mysql queries, one for each language, and then using php to merge the two.

 

So really, there is no real use for the super query.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.