I have a MySQL SQL query that selects products and the text translations:
SELECT *
FROM (select * from products) p
LEFT JOIN (SELECT * FROM translations) t
ON t.id = p.product_id
This grabs the products from the database. However it grabs all the available translations. I just want to get the translations in the current language. So i can use this:
SELECT *
FROM (select * from products) p
LEFT JOIN (SELECT * FROM translations WHERE lang = 'en') t
ON t.id = p.product_id
This works as intended, however not all products have a translation for every language. What I ultimately want to do is specify a language and if there is no given translation then return whatever other translations we have. Can this be done in SQL or am i going to need to iterate through the results in PHP?
If you create a ‘default’ language translation for each product, and give it code
'zz'then the following will work. There must be a'zz'translation for every product.(This took me literally an hour, but I’ve learned a lot in the process – great question!).
Sorry if it’s not efficient. You’ll need to try it out and see.