没有数据的例子很难理解最终的目标,但我想下面的变种之一是有用的。
使用IN
操作
,如果各部门和主题是恒定值,你可以使用IN
操作:
SELECT unit, Subject, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, marks
FROM questionbank
WHERE Department = "DCP" and Subject = "CO" and Unit IN ("1", "2", "3", "4", "5", "6") and marks IN ("1", "2", "3", "4")
不同的部门和主题
SELECT unit, Subject, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, marks
FROM questionbank
WHERE
(Department = "DCP" and Subject = "CO" and Unit = "1" and marks = "1")
OR (Department = "XXX" and Subject = "YY" and Unit = "2" and marks = "3")
OR ...
或者你可以使用其他一些方法t可能与上述两者中的一个类似。
首先,您需要自己决定这四个参数(部门,主题,单位和标记)如何相互关联。那么找到最佳解决方案会更容易。
更新:
对于每个标记不同的限制,我想这样的事情,可以用:
SELECT unit, Subject, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, marks
FROM questionbank
WHERE Department = "DCP" and Subject = "CO" and Unit IN ("1", "2", "3", "4", "5", "6") and marks = "1"
LIMIT 1
UNION ALL
SELECT unit, Subject, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, marks
FROM questionbank
WHERE Department = "DCP" and Subject = "CO" and Unit IN ("1", "2", "3", "4", "5", "6") and marks = "2"
LIMIT 2
UNION ALL
SELECT unit, Subject, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, marks
FROM questionbank
WHERE Department = "DCP" and Subject = "CO" and Unit IN ("1", "2", "3", "4", "5", "6") and marks = "3"
LIMIT 3
UNION ALL
SELECT unit, Subject, Question, OptionA, OptionB, OptionC, OptionD, CorrectAnswer, marks
FROM questionbank
WHERE Department = "DCP" and Subject = "CO" and Unit IN ("1", "2", "3", "4", "5", "6") and marks = "4"
LIMIT 4
但是,我无法检查该变种现在的语法是否正确。
你的问题是什么?你的查询看起来很好。 –
我刚刚写了一个单位的查询和标记= 1,我有6个这样的单位和标记1,2,3,4。如何将所有这些结合在一起? ,使用联合会使这个查询非常大,这又是另一种有效的方法吗? –
部门和主题是否因不同的单位和标记而有所不同?或者Department =“DCP”和Subject =“CO”'子句是常量? – ghostprgmr