如何在同一个表上查询不同的条件并加入结果

问题描述 投票:1回答:3

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

(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.并且限制由用户指定。这将是传统联盟方式中的安静大查询。如何以简单方便的方式做到这一点?

在此查询中,只有单位(1到6),标记(1到4)和限制是可变的。

mysql sql union
3个回答
0
投票

您的要求根本不明确,但您可以将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 

0
投票

没有数据示例很难理解最终目标,但我认为以下变体之一可能很有用。

使用IN运算符

如果Department和Subject是常量值,则可以使用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 ...

或者您可以使用其他一些类似于上述两种方法之一的方法。

首先,您需要自己决定这4个参数(部门,主题,单位和标记)如何与其他参数相关联。然后,找到最佳解决方案会容易得多。

更新:

对于每个标记的不同限制,我想可以使用这样的东西:

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

但是,我现在无法检查此变体以获得正确的语法。


0
投票

MySQL的优点在于它非常灵活,即使有多个表达式,IN子句也可以运行匹配。我无法找到关于我要解释的功能的好文档,但我会尽力简单地说明。

所以通常的IN子句就是这样的:

SELECT 
column1,column2,...
FROM
table_name
WHERE 
(expr|column_1) IN ('value1','value2',...);

但您可以修改IN子句表达式以使用所需数量的表达式:

SELECT 
column1,column2,...
FROM
table_name
WHERE 
((expr1|column_1),(expr2|column_2),..) IN (('value1','value2'), ('value3','value4')...);

我有一个简单的SQLFiddle证明了这一点。

因此,要简单回答您的问题,您可以像这样修改您的查询,以实现您想要的效果,而无需使用UNION过度查询您的查询。

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

我总是在我的查询中使用它,但没有看到很多人使用它。我觉得这是处理WHERE条款中配对组合的最佳方法。

© www.soinside.com 2019 - 2024. All rights reserved.