gpt4 book ai didi

mysql - 如何在同一张表上查询不同的where条件和连接结果

转载 作者:行者123 更新时间:2023-11-30 22:01:10 24 4
gpt4 key购买 nike

我有表格questionbank,我想根据不同的where条件提取问题,例如

(SELECT unit, Subject, Question, OptionA, OptionB, OptionC, OptionD , CorrectAnswer, marks 
FROM questionbank
WHERE Department = "DCP"
and Subject = "CO"
and Unit = "1"
and marks = "1" LIMIT 3)
UNION (SELECT unit,Subject,Question,OptionA ,OptionB ,OptionC ,OptionD ,CorrectAnswer,marks
FROM questionbank
WHERE Department ="DCP"
and Subject="CO"
and Unit="1"
and marks ="2" LIMIT 1 )

我有 6 个单元,标记为 1、2、3 或 4。并且限制由用户指定。这将是传统联合方式中安静的大查询。如何以简单易行的方式做到这一点?

只有 unit (1to6) , marks (1to4) 和 limit 在此查询中是可变的。

最佳答案

你的要求一点都不清楚,但是你可以在你的where中用or组合不同的可能条件,比如

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
(Unit = "2" and marks = "3") OR
/* ... */
(Unit = "6" and marks = "4")
)
LIMIT 3

关于mysql - 如何在同一张表上查询不同的where条件和连接结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43297400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com