sql - How to get multiple rows united by common column in mysql in complex select with join -


I have 2 tables, one page holding, the second with translated values ​​for some page properties:

Pages -id, in_menu, created_at ... etc

Page_translation - ID, page_id, locale, title, content ... Etc. (

Pages
  --- id -  

- title_en ---- Title_he --- In_Menu I have been using Larval (4.1) because I have done something:

  Page: (' Eg_translations ',' pages.id ',' = ',' page_translations.page_id ') -> select (array (' pages.id ',' page_translations.page_id as page id ',' page_translations.title as title_en ), 'Page_translations.title as title_he', 'pages.in_menu', 'pages.created_at')) - & gt; Where ('page_translations.locale', '=', 'N') - & gt; GroupB ('page_translations.page_id');   

The result is sql (those who do not dismiss the larval)

  select `pages`` `` `` `` `Page_translation ' In the form of `title_en`, 'Page_ translations` .` title as` title_he`, in the form of' Page_id `page id`,` page_ translations``` title``,   

pages . in_manu , page . pages from created_at to internal page_translations on pages . ID = page_translations . Page_ id where page_translations . Locale = 'n' group by page_translations . Now I have the title twice (as title_en and title_he) and so I used another query to replace title_ with the correct value

  PageTranslation :: wherePageId ($ pageId) - & gt; WhereLocale ('he') - & gt; Arc ('title')   

I want to know that anyone can make it in the same query call!

Just join page_t translation table twice:

  : Join ('page_translations as page_translations_en', function ($ included) at {$ join-> ('pages.id', '=', 'page_translations_en. (' Page_translations_en.locale ',' = ',' en ' ('Pages_id', '=', 'page_translations_he.page_id'); $ join- & gt; ('pages_id', '=', 'page_translations as page_translations_he', function ($ included) ('Page_translations_he.locale', '=', 'he')} - & gt; select (array ('page .id as page_id', 'page_translations_en.title as title_en', 'page_translations_he.title as title_he ',' Pages.in_menu ',' pages.created_at ')) - & gt; GroupB (' page_translations.page_id ');   

This is not guaranteed Areas that could work out of the box because I do not have a copy of your schema, but you get the idea.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -