我坚持使用这个mysql内连接查询

时间:2015-05-22 10:25:53

标签: mysql inner-join codeigniter-2

select t1.TITLE,PJT_ID, group_concat(t2.count) as COUNT, group_concat(t2.CREATED_BY) USERS,t2.task_id,group_concat(dc_users.PHOTO)as PHOTO from dc_tasks t1 inner join (SELECT dc_remarks.`CREATED_BY`,dc_sprints.PJT_ID , TSK_ID AS task_id, count( dc_remarks.`CREATED_BY` ) AS count FROM dc_remarks inner join dc_tasks on dc_remarks.`TSK_ID` = dc_tasks.ID inner join dc_sprints on dc_sprints.ID=dc_tasks.SPT_ID WHERE DATE( `CREATED_ON` ) = DATE('2015-05-22') AND dc_sprints.PJT_ID = GROUP BY `CREATED_BY` , `TSK_ID` ORDER BY `TSK_ID`) t2 on t1.ID = t2.task_id inner join dc_users on dc_users.FIRST_NAME = t2.CREATED_BY GROUP BY t2.task_id

2 个答案:

答案 0 :(得分:1)

您的错误在这里:

dc_sprints.PJT_ID = GROUP BY `CREATED_BY`.

在分组结果之前完成where条件

您的格式化的SQL看起来像这样:

select 
   t1.TITLE,PJT_ID, group_concat(t2.count) as COUNT, 
   group_concat(t2.CREATED_BY) 
   USERS,t2.task_id,group_concat(dc_users.PHOTO)as PHOTO 
from 
   dc_tasks t1 
inner join (
       SELECT 
           dc_remarks.`CREATED_BY`,dc_sprints.PJT_ID , 
           TSK_ID AS task_id, count( dc_remarks.`CREATED_BY` ) AS count 
       FROM 
           dc_remarks 
       inner join dc_tasks on dc_remarks.`TSK_ID` = dc_tasks.ID 
       inner join dc_sprints on dc_sprints.ID=dc_tasks.SPT_ID 
       WHERE 
            DATE( `CREATED_ON` ) = DATE('2015-05-22') AND 
           dc_sprints.PJT_ID = 
       GROUP BY `CREATED_BY` , `TSK_ID` ORDER BY `TSK_ID`
  ) t2 on t1.ID = t2.task_id 
  inner join dc_users on dc_users.FIRST_NAME = t2.CREATED_BY 
  GROUP BY t2.task_id

答案 1 :(得分:-1)

select 
   t1.TITLE,PJT_ID, group_concat(t2.count) as COUNT, 
   group_concat(t2.CREATED_BY) 
   USERS,t2.task_id,group_concat(dc_users.PHOTO)as PHOTO 
from 
   dc_tasks t1 
inner join (
       SELECT 
           dc_remarks.`CREATED_BY`,dc_sprints.PJT_ID , 
           TSK_ID AS task_id, count( dc_remarks.`CREATED_BY` ) AS count 
       FROM 
           dc_remarks 
       inner join dc_tasks on dc_remarks.`TSK_ID` = dc_tasks.ID 
       inner join dc_sprints on dc_sprints.ID=dc_tasks.SPT_ID 
       WHERE 
            DATE( `CREATED_ON` ) = DATE('2015-05-22') AND 
           dc_sprints.PJT_ID = dc_tasks.project_id
       GROUP BY `CREATED_BY` , `TSK_ID` ORDER BY `TSK_ID`
  ) t2 on t1.ID = t2.task_id 
  inner join dc_users on dc_users.FIRST_NAME = t2.CREATED_BY 
  GROUP BY t2.task_id