0

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

我已经看到这里这里使用子查询完成了这项工作。从评论来看,这种方法似乎是正确的,但它不适用于我的具体查询。我相信是因为我有几个JOINs,但我不确定。我可以让查询无错误地运行,但是当我完全按照上述帖子中的说明添加行号时,“排名”列出现故障。

这是我的原始查询,没有添加排名:

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[...]

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

我究竟做错了什么?

4

1 回答 1

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 ;
于 2013-01-05T22:39:43.757 回答