选择++连接时的情况,MYSQL

时间:2018-09-28 09:36:20

标签: mysql left-join case

在以下这种情况下,您能为我提供帮助吗? 我想选择并更改结果:

我的代码如下:

$USER_STORY = $this->lang->line('application_status_change_user_story');
$TO_DO = $this->lang->line('application_status_change_to_do');
$IN_PROGRESS = $this->lang->line('application_status_change_in_progress');
$DONE = $this->lang->line('application_status_change_done');
$UNDONE = $this->lang->line('application_status_change_undone');

    SELECT sum(project_has_tasks.estimated_hours) as val, project_has_tasks.name, 
    project_has_tasks.start_date,
    project_has_tasks.due_date,
    project_has_tasks.progress,
    TIME_FORMAT(SEC_TO_TIME(sum(project_has_tasks.time_spent)),'%k.%i') 
    as total_time, users.id, users.firstname, users.lastname,(case
             when 'user_story' then '$USER_STORY'
             when 'to_do' then '$TO_DO'
             when 'in_progress' then '$IN_PROGRESS'
             when 'done' then '$DONE'
             when 'undone' then '$UNDONE'
             else 'NOTHING'
          end) as status
    FROM project_has_tasks
    RIGHT JOIN users ON project_has_tasks.user_id
     = users.id WHERE project_has_tasks.project_id ='99' 
    GROUP BY project_has_tasks.id ASC

需要修改什么才能获得适当的结果?现在,我在状态列中什么都没有,但是没有错误。

1 个答案:

答案 0 :(得分:0)

我认为您的case语句有误,没有条件在检查,因此仅返回最后一个else语句

SELECT sum(project_has_tasks.estimated_hours) as val, project_has_tasks.name, 
project_has_tasks.start_date,
project_has_tasks.due_date,
project_has_tasks.progress,
TIME_FORMAT(SEC_TO_TIME(sum(project_has_tasks.time_spent)),'%k.%i') 
as total_time, users.id, users.firstname, users.lastname,(case
         when project_has_tasks.status = 'user_story' then '$USER_STORY'
         when project_has_tasks.status = 'to_do' then '$TO_DO'
         when project_has_tasks.status = 'in_progress' then '$IN_PROGRESS'
         when project_has_tasks.status = 'done' then '$DONE'
         when project_has_tasks.status = 'undone' then '$UNDONE'
         else 'NOTHING'
      end) as status
FROM project_has_tasks
RIGHT JOIN users ON project_has_tasks.user_id
 = users.id WHERE project_has_tasks.project_id ='99' 
GROUP BY project_has_tasks.id ASC