SELECT *
FROM
(
SELECT com_jobcard.job_card_num,
sum( worked_qty ),employee.emp_name
FROM timer_completed
INNER JOIN process ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON com_jobcard.id = timer_completed.job_card_id
INNER JOIN employee ON employee.id = timer_completed.employee_id
AND process.id = '611'
AND timer_completed.group_id = '60'
AND timer_completed.report_date = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
ORDER BY com_jobcard.id DESC
) AS tmp_table
GROUP BY com_jobcard.job_card_num
在此代码中,我使用Group by
选项,但如果我使用上面的查询返回,我需要com_jobcard.id
的降序结果:
#1054
- 未知栏' com_jobcard.job_card_num'在'组 声明'
请帮帮我。
答案 0 :(得分:0)
使用
GROUP BY tmp_table.job_card_num
答案 1 :(得分:0)
两件事: 1)子查询中的所有列都需要命名。这将清除错误#1054
Foo.SOME_CONSTANT_STRING
2)如果使用“Top Select”子句,则Order by仅在Subqueries中可用。您需要使用Order by Where by group by,反之亦然
答案 2 :(得分:0)
您在这里不必要地嵌套查询。您可以通过将ORDER BY
放在GROUP BY
之后来订购汇总查询结果集。此外,就像戈登在评论中指出的那样,你正在滥用非标准的MySql扩展名GROUP BY
。除非你了解它,否则这会让你发疯。 https://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html
尝试重构您的查询,如下所示:
SELECT com_jobcard.job_card_num,
sum( worked_qty),
employee.emp_name,
com_jobcard.id
FROM timer_completed
JOIN process ON process.id = timer_completed.process_id
JOIN com_jobcard ON com_jobcard.id = timer_completed.job_card_id
JOIN employee ON employee.id = timer_completed.employee_id
AND process.id = '611'
AND timer_completed.group_id = '60'
AND timer_completed.report_date = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
GROUP BY com_jobcard.job_card_num, employee.emp_name, com_jobcard.id
ORDER BY com_jobcard.id DESC
除了比你提出的查询更简单之外,它正确处理GROUP BY
并产生你指定的顺序。