MySQL: How to associate a column alias with a specific JOIN clause

放肆的年华 提交于 2019-12-23 22:26:06

问题


My query pulls results from several tables using a few LEFT JOINs. It currently works fine. However, I now have to get two different fields from the same table column (but matching different conditions). I don't know how to make sure the correct column alias gets assigned from the correct LEFT JOIN. Make sense?

So here's for example...

SELECT table1.user_id, table2.value AS school, table2.value AS language  FROM table1  
LEFT JOIN table2 ON table2.user_id = table1.user_id AND table2.key = 'school'
LEFT JOIN table2 ON table2.user_id = table1.user_id AND table2.key = 'language'

I need the column aliases with the corresponding JOIN. How do I do this?

For reference, here is my original query (without secondary JOIN):

SELECT wsat_ib.user_id, wpjb_resume.id resume_id, wpjb_resume.firstname, wpjb_resume.lastname, 
     wsat_ib.overall_score, wsat_ib.account_score, wsat_ib.econ_score, wsat_ib.math_score,
     wsat_ib.logic_score, wsat_ib.fsanaly_score, wsat_ib.corpval_score, wsat_ib.end_time, 
     GROUP_CONCAT(DISTINCT wp_usermeta.meta_value) AS target_employers, wpjb_field_value.value AS school, 
     GROUP_CONCAT(wpjb_application.job_id) AS applications FROM  `wsat_ib` 
    LEFT JOIN wp_usermeta ON wsat_ib.user_id = wp_usermeta.user_id
    LEFT JOIN wpjb_resume ON wsat_ib.user_id = wpjb_resume.user_id
    LEFT JOIN wpjb_field_value ON wpjb_resume.id=wpjb_field_value.job_id AND wpjb_field_value.field_id=3
    LEFT JOIN wpjb_application ON wpjb_application.user_id = wsat_ib.user_id 
    WHERE (wp_usermeta.meta_key = 'target_employer' AND (wp_usermeta.meta_value = 'public' OR wp_usermeta.meta_value=1523) AND wpjb_resume.is_active =1) AND (wpjb_resume.firstname='Xu' OR wpjb_resume.lastname='Xu') 
    GROUP BY wsat_ib.user_id, resume_id, wpjb_resume.firstname, wpjb_resume.lastname, wsat_ib.overall_score, wsat_ib.account_score, wsat_ib.econ_score, wsat_ib.math_score, wsat_ib.logic_score, wsat_ib.fsanaly_score, wsat_ib.corpval_score, wsat_ib.end_time, wpjb_field_value.value
     ORDER BY overall_score DESC LIMIT 0, 20;

Here's my early attempt at implementing the solutions below. I've added the AS school to the JOIN clause, but have not yet added the second JOIN for that table. For some reason, this version returns no results.

SELECT wsat_ib.user_id, wpjb_resume.id resume_id, wpjb_resume.firstname, wpjb_resume.lastname, 
 wsat_ib.overall_score, wsat_ib.account_score, wsat_ib.econ_score, wsat_ib.math_score,
 wsat_ib.logic_score, wsat_ib.fsanaly_score, wsat_ib.corpval_score, wsat_ib.end_time, 
 GROUP_CONCAT(DISTINCT wp_usermeta.meta_value) AS target_employers, wpjb_field_value.value AS school, 
 GROUP_CONCAT(wpjb_application.job_id) AS applications FROM  `wsat_ib` 
LEFT JOIN wp_usermeta ON wsat_ib.user_id = wp_usermeta.user_id
LEFT JOIN wpjb_resume ON wsat_ib.user_id = wpjb_resume.user_id
LEFT JOIN wpjb_field_value AS school ON wpjb_resume.id=wpjb_field_value.job_id AND wpjb_field_value.field_id=3
LEFT JOIN wpjb_application ON wpjb_application.user_id = wsat_ib.user_id 
WHERE (wp_usermeta.meta_key = 'target_employer' AND (wp_usermeta.meta_value = 'public' OR wp_usermeta.meta_value=1523) AND wpjb_resume.is_active =1) AND (wpjb_resume.firstname='Xu' OR wpjb_resume.lastname='Xu') 
GROUP BY wsat_ib.user_id, resume_id, wpjb_resume.firstname, wpjb_resume.lastname, wsat_ib.overall_score, wsat_ib.account_score, wsat_ib.econ_score, wsat_ib.math_score, wsat_ib.logic_score, wsat_ib.fsanaly_score, wsat_ib.corpval_score, wsat_ib.end_time, wpjb_field_value.value
 ORDER BY overall_score DESC LIMIT 0, 20;

回答1:


You need to alias the tables as well, not just the columns.

SELECT table1.user_id, schools.value AS school, languages.value AS language
FROM table1  
LEFT JOIN table2 AS schools ON schools.user_id = table1.user_id
    AND schools.key = 'school'
LEFT JOIN table2 AS languages ON languages.user_id = table1.user_id
    AND languages.key = 'language'

If a table appears more than once in a query, you can think of each appearance kind of as an instance. You need to name each instance of the table to disambiguate them.




回答2:


LEFT JOIN table2 as t2 ON t2.id = ...
                ^^^^^^^

The as can be omitted, as in:

LEFT JOIN table2 t2 ON t2.id = ...


来源:https://stackoverflow.com/questions/14183817/mysql-how-to-associate-a-column-alias-with-a-specific-join-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!