我有一个mysql查询,我需要在Group by语句中添加一些条件,如果我在Group中使用单个字段可以工作但我需要在Group by中包含两个字段,这里是我的查询任何一个请帮助我找出问题
SELECT (CASE
WHEN CSR.skill_type = 1 THEN
(SELECT skills_value from cv_skills
WHERE skills_id = CSR.skill_id )
WHEN CSR.skill_type = 2 THEN
(SELECT ostype_name from cv_os_type
WHERE ostype_id = CSR.skill_id )
WHEN CSR.skill_type = 3 THEN
(SELECT dbtype_name from cv_db_type
WHERE dbtype_id = CSR.skill_id)
WHEN CSR.skill_type = 4 THEN
(SELECT title from candidate_competencies
WHERE id = CSR.skill_id)
WHEN CSR.skill_type = 0 THEN
IT.type_name
END) AS skill_name,
(CASE
WHEN IT.type_parent_id > 0 THEN
IT.type_parent_id
WHEN IT.type_parent_id = 0 THEN
CIS.interview_type
END) AS typeId,
(CASE
WHEN CSR.skill_type = 4 THEN
minimum_rating
END) AS minimum_rating,
AVG( CSR.rate ) AS skill_rating,
CSR.skill_type,CIS.created, CC.id
FROM `candidate_interview_skill_rate` `CSR`
LEFT JOIN `candidate_interview_process` `CIP` ON CSR.interview_process_id = CIP.id
LEFT JOIN `candidate_interview_schedule` `CIS` ON CIS.id = CIP.interview_schedules_id AND CIS.archive_date IS NULL
LEFT JOIN `candidate_interview` `CI` ON CI.id = CIS.interview_id AND CI.archived_date IS NULL
LEFT JOIN `interview_type` `IT` ON IT.id = CIS.interview_type
LEFT JOIN `candidate_competencies` `CC` ON CC.id = CSR.skill_id
WHERE CI.candidate_user_id = 39
GROUP BY (CASE
WHEN CSR.skill_type > 0 THEN
CSR.skill_id, CSR.skill_type
ELSE CIS.interview_type
END)
ORDER BY `CSR`.`skill_type`
答案 0 :(得分:4)
您可以将CSR.skill_id,CSR.skill_type连接到单个列,并在select语句中使用它。该小组需要像这样
GROUP BY CASE
WHEN CSR.skill_type > 0 THEN
CONCAT(CSR.skill_id, '-', CSR.skill_type)
ELSE
CIS.interview_type
END;
希望这有效。
答案 1 :(得分:1)
您可以在GROUP BY子句中包含多个表达式,只需用逗号分隔表达式,就像在SELECT列表中一样。
通常,SELECT列表中的表达式在GROUP BY子句中重复。
通常可以使用CASE表达式来支持更复杂的条件。
很难为查询,缺少样本数据和所需输出提供更具体的帮助。