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

时间:2017-04-08 16:16:01

标签: mysql sql union

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

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

此查询中只有单位(1to6),标记(1to4)和限制是可变的。

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:0)

您的要求根本不明确,但您可以将whereor中的不同条件结合起来,例如

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 

答案 2 :(得分: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子句中配对组合的最佳方法。