如果分数等于maxscore,我想创建第三个选择,否则将失败。这是要评估的查询......
我该怎么做?我可以创建第三个子查询AS状态,还是需要在变量上创建??
SELECT DISTINCT
qui.title AS Course_Name,
(SELECT sum(score)
FROM jos_jquarks_quizzes_answersessions
WHERE score IS NOT NULL
AND quizsession_id = quizSession.id
AND status <> -1 ) AS score,
(SELECT count(distinct question_id)
FROM jos_jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id) AS maxScore,
(SELECT count(distinct question_id)
FROM jos_jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id ) AS QuizStatus,
DATE_FORMAT(quizSession.finished_on,'%W, %M %e, %Y @ %h:%i %p') As Finished
FROM
jos_jquarks_quizsession AS quizSession
LEFT JOIN
jos_jquarks_users_quizzes AS users_quizzes ON users_quizzes.id = quizSession.affected_id
LEFT JOIN
jos_jquarks_quizzes AS qui ON users_quizzes.quiz_id = qui.id
LEFT JOIN
jos_jquarks_quizzes_answersessions AS quizSessAns ON quizSessAns.quizsession_id = quizSession.id
LEFT JOIN
jos_jquarks_sessionwho AS sessionWho ON sessionWho.session_id = quizSession.id
LEFT JOIN
jos_jquarks_users_profiles AS users_profiles ON users_profiles.user_id = sessionWho.user_id
LEFT JOIN
jos_jquarks_profiles AS profiles ON profiles.id = users_profiles.profile_id
WHERE
sessionWho.user_id = '246'
答案 0 :(得分:1)
如果您不需要返回得分或maxscore,那么您只需比较两个子查询表达式:
SELECT quizSession.id, IF(
(SELECT sum(score)
FROM jos_jquarks_quizzes_answersessions
WHERE score IS NOT NULL AND quizsession_id = quizSession.id AND status <> -1) =
(SELECT count(distinct question_id)
FROM jos_jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id), "Pass", "Fail") AS status
FROM quizSession
如果确实需要所有列,那么将子查询编写为连接可能会更好:
SELECT quizSession.id, sum_subquery.score_sum AS score, max_subquery.max_score,
IF(sum_subquery.score_sum = max_subquery.max_score, "Pass", "Fail") AS status
FROM quizSession
INNER JOIN (
SELECT quizsession_id, sum(score) AS score_sum
FROM jos_jquarks_quizzes_answersessions
WHERE score IS NOT NULL AND status <> -1
GROUP BY quizSession_id
) AS sum_subquery ON quizSession.id = sum_subquery.quizsession_id
INNER JOIN (
SELECT quizsession_id, count(distinct question_id)
FROM jos_jquarks_quizzes_answersessions
GROUP BY quizsession_id
) AS max_subquery ON quizSession.id = max_subquery.quizsession_id
答案 1 :(得分:0)
SELECT * from
(SELECT sum(score)
FROM jos_jquarks_quizzes_answersessions
WHERE score IS NOT NULL AND quizsession_id = quizSession.id
AND status <> -1
UNION
SELECT count(distinct question_id) FROM jos_jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id
UNION
SELECT count(distinct question_id) FROM jos_jquarks_quizzes_answersessions
WHERE quizsession_id = quizSession.id ) AS marksscormaxxmarkstrigger
这很好用