简体   繁体   中英

How to give CASE WHEN THEN in mysql where clause based on select columns?

I am using select query and selection columns in which i have to get total count of a column as well as condition based count

for total count column name in select query is ims_total_count (condition is_correct IN (1,0)) for column name ims_code_count condition is_correct = 0

i paste my code below

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;

This is works fine for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM