So I have 3 tables
User Table
UserId User
Language Table
LanguageId Language Fluency
User Language Table
UserLanguageId UserId LanguageId
Basically what I need is a query where PriLang would be where Fluency=’Primary’ and SecLang is Fluency=’Secondary’ and looks somethin like this
+------------+-----------------+-----------+ | User | PriLang | SecLang | +------------+-----------------+-----------+ | Jimbo | English | Spanish | +------------+-----------------+-----------+ | Norm | French | Spanish | +------------+-----------------+-----------+ | Kathy | Japanese | Italian | +------------+-----------------+-----------+
Advertisement
Answer
In my view, the most straightforward approach involves two joins to the user_languages table, as well as a join to the langauges table for each of them.
select usr.User, lg1.Language PriLang, lg2.Language SecLang from users usr left join user_languages ul1 on ul1.userId = usr.userId and ul1.Fluency='Primary' left join user_languages ul2 on ul2.userId = usr.userId and ul2.Fluency='Secondary' left join languages lg1 on lg1.languageId = ul1.languageId left join languages lg2 on lg2.languageId = ul2.languageId