如何在基于选择列的mysql where子句中提供CASE WHEN THEN?

时间:2019-06-20 08:05:54

标签: php mysql

我正在使用选择查询和选择列,其中我必须获取列的总计数以及基于条件的计数

对于选择查询中的总计数列名称为ims_total_count(条件为is_correct IN(1,0)) 列名称ims_code_count条件is_correct = 0

我在下面粘贴我的代码

SELECT q.ims_code, 
       COUNT(q.ims_code) AS ims_total_count, 
       M.NAME, 
       M.score_board_order, 
       M.id AS exam_id, 
       M.view_question_hr , 
       M.section_id, 
       GROUP_CONCAT(DISTINCT M.section_id) AS coded_section,
       GROUP_CONCAT(DISTINCT REPLACE(SUBSTRING_INDEX(M.NAME,'Sec', 1) ,'New ','') ) AS coded_test_name,
       COUNT(q.ims_code) AS ims_code_count,
       q.question_password, 
       qcat.NAME  AS topic_name,
       qsubcat.NAME AS cat_name,
       qsubcat.description AS sub_cat_name,
       GROUP_CONCAT( DISTINCT UPPER( REPLACE(LOWER(q.question_code),'new','') ) ) AS coded_que,
       GROUP_CONCAT( DISTINCT q.id ) AS coded_que_id,
       GROUP_CONCAT(q.exam_id) AS coded_exam_id
  FROM y2a_watupro_question AS q INNER JOIN y2a_watupro_master AS M 
    ON  q.exam_id = M.id LEFT JOIN y2a_watupro_qcats AS qcat 
    ON  q.cat_id = qcat.id LEFT JOIN y2a_watupro_qsub_cats AS qsubcat 
    ON  q.sub_cat_id = qsubcat.id 
 WHERE q.id IN ( 
     SELECT question_id 
       FROM y2a_watupro_student_answers 
      WHERE CASE is_correct WHEN ims_total_count THEN '1,0' 
       ELSE '0' END AND taking_id IN ( 
          SELECT t.id 
          FROM y2a_watupro_taken_exams AS t INNER JOIN y2a_test_score_board_section AS s 
            ON  s.id = t.section_id 
         WHERE s.sc_id =0 
           AND t.user_id=5270 
           AND t.trial_status = 1 
           AND t.in_progress = 0 
           AND t.isactive = 1 ) 
              ) 
    GROUP BY M.score_board_order, q.ims_code 
 ORDER BY M.score_board_order ASC;

1 个答案:

答案 0 :(得分:0)

SELECT *,CASE
    WHEN OCCUPATION = 'I am Salaried' THEN 'Salaried'
    WHEN OCCUPATION = 'I am not Employed' THEN 'Student'
    WHEN OCCUPATION = 'I am Retired' THEN 'Retired'
    WHEN OCCUPATION = 'I am Self Employed' THEN 'Self Employed'
    WHEN OCCUPATION = 'I Run Business' THEN 'Own Business'
    WHEN OCCUPATION = 'I am a Professional (Dr/CA/LLB/Engg etc)' THEN 'Professional'
    ELSE 'OCCUPATION'
END AS OCCUPATION FROM TABLE_NAME;

这对我来说很好。