14

I have two table with this structure

categories
--------------------------------
id  | create
1   | 2012-12-01 12:00:00
2   | 2012-12-01 12:00:00

category_i18ns
--------------------------------
category_id     | lang  | title
1               | en    | home page
1               | de    | HomE PAGEa
1               | fa    | خانه
2               | en    | about

some categories have all languages data and some another havnt. for example category id 2 just have en translated title. i want to have a query that first check the current language for retrieve title and then if not exist then show another ones.

i have a query to get current category data with i18n data in this current example de is default language. for category id 1 we have de title but for category 2 we dont. so i want english version as title of category

SELECT * FROM `mhf_swe_ndzhmju3_test`.`categories` AS `Category`
LEFT JOIN `mhf_swe_ndzhmju3_test`.`category_i18ns` AS `CategoryLocale` ON (
`CategoryLocale`.`category_id` = `Category`.`id`
AND `CategoryLocale`.`lang` = 'de')

desire out put for 'de' language

Category.id     CategoryLocale.title    CategoryLocale.lang
1               HomE PAGEa              de
2               about                   en
4
  • If you have en and fa for category 2 but haven't de which language you must choose? Dec 11, 2012 at 9:48
  • Can you please mention properly the output you expect ?
    – Subodh
    Dec 11, 2012 at 9:50
  • @HamletHakobyan not important. first that mysql serverd Dec 11, 2012 at 9:50
  • desire output added. please check and suggest the query. ty Dec 11, 2012 at 9:57

3 Answers 3

22
SELECT Category.id,
        COALESCE(a.lang, b.lang) lang,
        COALESCE(a.title, b.title) title
FROM   categories AS Category
      LEFT JOIN category_i18ns AS a
           ON a.category_id = Category.id AND a.lang = 'de'
      LEFT JOIN category_i18ns AS b
           ON b.category_id = Category.id AND b.lang = 'en'
7
  • if we dont know which language added to the special category ? how can i retrive them. for example category id 3 added just via fr language and dont have any another language Dec 11, 2012 at 10:05
  • Aren't you making an assumption here that you will have an entry for en in all the categories ?
    – Subodh
    Dec 11, 2012 at 10:05
  • @sub_stantial nope .. there is no major default language for all category. Dec 11, 2012 at 10:11
  • @sweb : Yes i agree. Anyway my question was for Kuya John.
    – Subodh
    Dec 11, 2012 at 10:14
  • so what's the query guys :D Dec 11, 2012 at 10:15
4

If you have an order of preference, left join all languages and put them to coalesce in order of preference:

SELECT Category.id,
    COALESCE(a.lang, b.lang, c.lang, d.lang) lang,
    COALESCE(a.title, b.title, c.title, d.title) title
FROM   categories AS Category
  LEFT JOIN category_i18ns AS a
       ON a.category_id = Category.id AND a.lang = 'de'
  LEFT JOIN category_i18ns AS b
       ON b.category_id = Category.id AND b.lang = 'en'
  LEFT JOIN category_i18ns AS c
       ON c.category_id = Category.id AND c.lang = 'fr'
  LEFT JOIN category_i18ns AS d
       ON d.category_id = Category.id AND d.lang = 'es';

If you want to get a text from a random language, use this query:

SELECT Category.id,
        COALESCE(a.lang, b.lang) lang,
        COALESCE(a.title, b.title) title
FROM   categories AS Category
      LEFT JOIN category_i18ns AS a
           ON a.category_id = Category.id AND a.lang = 'de'
      LEFT JOIN category_i18ns AS b
           ON b.category_id = Category.id
GROUP BY Category.id

http://sqlfiddle.com/#!2/2beaf/5

1

Okay, this is not the best solution, but I think should work fine :

SELECT * FROM categories c INNER JOIN category_i18ns t1 
ON c.id = t1.category_id AND t1.lang = 'de'

UNION 

SELECT * FROM categories c LEFT JOIN category_i18ns t2 
ON c.id = t2.category_id and t2.lang != 'de' 
WHERE t2.category_id not in
   (SELECT DISTINCT category_id FROM category_i18ns WHERE lang = 'de') 
GROUP BY c.id

SQL Fiddle Example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.