Php 在基于select列的mysql where子句中如何给出CASE?

Php 在基于select列的mysql where子句中如何给出CASE?,php,mysql,Php,Mysql,我使用的是select查询和选择列,在这些列中,我必须获得列的总计数以及基于条件的计数 对于总计数,select查询中的列名为ims_total_count(条件在(1,0)中正确) 对于列名ims\U code\U count条件为\U CORPORT=0 我把代码贴在下面 SELECT q.ims_code, COUNT(q.ims_code) AS ims_total_count, M.NAME, M.score_board_order,

我使用的是select查询和选择列,在这些列中,我必须获得列的总计数以及基于条件的计数

对于总计数,select查询中的列名为ims_total_count(条件在(1,0)中正确) 对于列名ims\U code\U count条件为\U CORPORT=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;
这对我来说很好


这对我来说很好。

你能解释一下为什么这是最好的方式吗?我不知道,它是否是最好的方式,但这给了我所需的输出。你能解释一下为什么这是最好的方式吗?我不知道,它是否是最好的方式,但这给了我所需的输出。
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;