MySQL:如何使用LEFT JOIN对行进行编号

时间:2013-01-05 21:53:21

标签: mysql sql

我的MySQL查询是针对报告的,所以我想对每一行进行编号。换句话说,我想添加一个像“rank”这样的字段,第一行将是1,第二行将是等等。

我已经使用子查询herehere看到了这一点。从评论来看,这种方法似乎是正确的,但它不适用于我的特定查询。我相信这是因为我有几个JOIN,但我不确定。我可以让查询无错误地运行,但是当我按照上述帖子中的说明完全添加行号时,“rank”列就会出现故障。

这是我的原始查询,未添加等级:

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=2) AND wpjb_resume.is_active =1)
    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 end_time DESC LIMIT 0, 20

这是我尝试添加排名字段的失败:

SELECT @row := @row + 1 as row, 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 (SELECT @row := 0) r, `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=2) AND wpjb_resume.is_active =1)
GROUP BY wsat_ib.user_id, resume_id, wpjb_resume.firstnam[...]

查询运行没有错误,但是rank列不是1,2,3,4 ......而是它:496,498,497,499 ...

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的行号与预期不符,因为您的SQL查询中包含GROUP BY子句。在对每行进行分组之前,会对SELECT行进行评估。

要解决您的问题,您可以使用嵌套查询,例如:

SELECT *, @row := @row + 1 AS row FROM (SELECT ...) AS t;

您提供的查询将是:

SELECT *, @row := @row + 1 AS row FROM (
    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=2) AND wpjb_resume.is_active =1)
        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 end_time DESC LIMIT 0, 20) AS t
  CROSS JOIN
    (SELECT @row := 0) AS r 
ORDER BY end_time DESC ;