Mysql组由普通列和mysql函数别名列组成

时间:2016-02-08 12:11:33

标签: mysql database

我一直试图通过mysql组在正常列和mysql函数别名列中进行分组,其中mysql函数别名不会工作分组 我试过的查询,应该根据mysql文档工作。

SELECT `project`.`project_name`, SUM(cost_allocated) AS cost_allocated,
    count(task_id) as countTask, 
    GROUP_CONCAT(task_registration.members SEPARATOR ';') AS members,     
    GROUP_CONCAT(task_registration.indicators SEPARATOR ';') AS indicators, 
    GROUP_CONCAT(task_registration.percent_complete SEPARATOR ';') AS percent_complete,
    GROUP_CONCAT(task_registration.status SEPARATOR ';') AS status, 
    DATE_FORMAT(`task_registration`.`created_at`, '%M') AS month
FROM (`project`) 
INNER JOIN `task_registration` ON `task_registration`.`project` = `project`.`project_id` 
GROUP BY `task_registration`.`project`,`month` 
ORDER BY `project`.`project_id` desc

我得到的结果 enter image description here

现在删除正常的列组后,只需添加mysql函数别名列,它工作正常,但我需要两个

SELECT `project`.`project_name`, SUM(cost_allocated) AS cost_allocated, 
    count(task_id) as countTask, 
    GROUP_CONCAT(task_registration.members SEPARATOR ';') AS members, 
    GROUP_CONCAT(task_registration.indicators SEPARATOR ';') AS indicators, 
    GROUP_CONCAT(task_registration.percent_complete SEPARATOR ';') AS percent_complete, 
    GROUP_CONCAT(task_registration.status SEPARATOR ';') AS status, 
    DATE_FORMAT(`task_registration`.`created_at`, '%M') AS month
FROM (`project`) 
INNER JOIN `task_registration` ON `task_registration`.`project` = `project`.`project_id` 
GROUP BY `month` 
ORDER BY `project`.`project_id` desc
从群组中删除task_registration`.`project之后

enter image description here

 GROUP BY `task_registration`.`project`,`month`  Doesnt work in this case

http://sqlfiddle.com/#!2/18389/2我到目前为止已尝试过这个

2 个答案:

答案 0 :(得分:1)

如果没有表声明,示例输入数据和示例输出数据,很难找到你想要的东西,但是从我的评论中我可以建议使用第二个查询作为子查询并将其加入第一个查询的结果: -

^require

答案 1 :(得分:0)

谢谢你我自己做的答案

   SELECT COUNT( i.project_name ) AS completed_projects,i.month 
FROM (

SELECT `project`.`project_name` , SUM( cost_allocated ) AS cost_allocated, COUNT( task_id ) AS countTask, GROUP_CONCAT( task_registration.members
SEPARATOR  ';' ) AS members, GROUP_CONCAT( task_registration.indicators
SEPARATOR  ';' ) AS indicators, GROUP_CONCAT( task_registration.percent_complete
SEPARATOR  ';' ) AS percent_complete, GROUP_CONCAT( task_registration.status
SEPARATOR  ';' ) AS 
STATUS , DATE_FORMAT( task_registration.created_at,  '%M' ) AS 
MONTH FROM (
`project`
)
INNER JOIN  `task_registration` ON  `task_registration`.`project` =  `project`.`project_id` 
GROUP BY  `task_registration`.`project` 
ORDER BY  `project`.`project_id` DESC
) AS i
GROUP BY i.month

http://sqlfiddle.com/#!2/18389/11