我需要在WHERE
子句中添加一个字段,并在我的存储过程中添加一个条件,那么在CASE
条件下,以下查询的正确语法是什么?
OR条件为我提供了以下条件0,1,2,3,4
的所有记录(如果找不到,我需要tqm.is_imp = 1
tqm.is_imp IN (0,2,3,4))
。我该怎么办?请帮忙。
tqm.is_imp = 1 OR tqm.is_imp IN (0,2,3,4)
我的查询:
SELECT tqm.id INTO selectedQuestionNumber
FROM tc_question_master tqm
INNER JOIN tc_question_mapping tqmap ON tqmap.tc_question_id = tqm.id
WHERE tqmap.syllabus_chapter_details_id IN (SELECT cms_id from topic where chapter_id IN (SELECT id from MyChapters))
AND tqm.marks = CurMark
CASE WHEN CurQuestionType > 0 THEN
AND tqm.question_type = CurQuestionType
END
AND (tqm.level_of_question = 'Easy' OR tqm.level_of_question
IN ('Moderate','Difficult','Very difficult'))
AND (tqm.is_imp = 1 OR tqm.is_imp IN (0,2,3,4))
AND tqm.id NOT IN (SELECT q.question_id FROM QBDetails q)
ORDER BY RAND() LIMIT 1;
答案 0 :(得分:0)
上半部分只是一个重新格式化,但当你接近一个案例表达式的逻辑时,它会更低,大部分都是纯粹的猜测。
SELECT
tqm.id INTO selectedQuestionNumber
FROM tc_question_master tqm
INNER JOIN tc_question_mapping tqmap ON tqmap.tc_question_id = tqm.id
WHERE tqmap.syllabus_chapter_details_id IN (
SELECT
cms_id
FROM topic
WHERE chapter_id IN (
SELECT
id
FROM MyChapters
)
)
AND (
tqm.level_of_question = 'Easy'
OR tqm.level_of_question IN ('Moderate', 'Difficult', 'Very difficult')
)
AND (
tqm.is_imp = 1
OR tqm.is_imp IN (0, 2, 3, 4)
)
AND tqm.id NOT IN (
SELECT
q.question_id
FROM QBDetails q
)
## now it is really vague
AND (
tqm.marks = CurMark
OR
(CurQuestionType > 0 AND tqm.question_type = CurQuestionType)
)
## end of guesswork
ORDER BY
RAND()
LIMIT 1;