简体   繁体   中英

MOODLE MySQL using 'and' in SELECT part of statement

In MOODLE, I'm using the following script to pull some numbers -- and they come out fine:

SELECT
qc.name,
q.category,
SUM(IF(qs.grade = "1",1,0)) AS Correct,
SUM(IF(qs.grade = "0",1,0)) AS Wrong

FROM
mdl_question_states qs,
mdl_quiz_attempts qa,
mdl_quiz qz,
mdl_course c,
mdl_question q,
mdl_question_categories qc

WHERE
qa.id = qs.attempt
AND qs.event = 6
AND qa.quiz = qz.id
AND ((qz.name = 'Pre-Test') OR (qz.name = 'Post-Test'))
AND qz.course = c.id
AND q.id = qs.question
AND q.category = qc.id
AND q.category > 601

GROUP BY q.category

ORDER BY qc.name

My question is this: I want to have a column (after the 'SUM(IF...' columns) that are 'Correct -AND- Pre-Test' followed by 'Correct -AND- Post-Test'.

What is the syntax to use to accomplish this?

Try

SUM(IF(sq.grade = '1' AND qz.name='pre-test', 1, 0)) AS cor_and_pre

and similarly for the other value.

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