【问题标题】:Select column when IF is true当 IF 为真时选择列
【发布时间】:2016-12-23 15:53:04
【问题描述】:

我有这样的场景..

元值表

ID |  question_id | m_id | m_value
----------------------------------
1  |  1           |  1   |  Easy 
2  |  1           |  2   |  Physics
3  |  2           |  1   |  Hard 
4  |  2           |  2   |  English

元表

ID |  Meta
------------
1  |  Difficulty
2  |  Subject

我想编写一个存储过程,当 Difficulty=Easy AND Subject = English 时选择一行。有任何想法吗。? 这是我到目前为止所做的

BEGIN

SELECT
    a.q_id,
    a.q_text,
    b.answer , 
    IF(
        c.m_id=1 AND c.m_value = d,
        c.m_value,
        IF(
            c.m_id=2 AND c.m_value=s,
            m_value,
            m_value
        )
    ) as selection
FROM
    question_pool AS a,
    answers_table AS b,
    meta_prop_values AS c
WHERE
    a.q_id = b.q_id
    AND
    a.q_id = c.q_id    
ORDER BY
    Rand() LIMIT n;

这是输出。但这并不是我想要的

 q_id           q_text      answer    selection
--------------------------------------------------------------------------
    7   <p>Another difficult question for English</p>   Difficult 3 0
    12  <p>Physics Equations 4&nbsp;<span class="math-tex">\(x = {{b^2-4ac} \over 2a}\)</span></p>  Easy 2  Physics
    14  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span> when a =5 b = 5 c= 10</p> 14  Hard
    12  <p>Physics Equations 4&nbsp;<span class="math-tex">\(x = {{b^2-4ac} \over 2a}\)</span></p>  Easy 1  Physics
    6   <p>This is a easy english question</p>  Easy 03 0
    6   <p>This is a easy english question</p>  Easy O2 English
    16  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span>&nbsp;when a =20&nbsp;b = 20&nbsp;c=-40</p>    457 0
    5   <p>This is another Question with Demo data</p>  Nothing 0
    5   <p>This is another Question with Demo data</p>  Cravitto    English
    15  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span>&nbsp;when a =10&nbsp;b = 10&nbsp;c=-40</p>    45  Hard
    15  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span>&nbsp;when a =10&nbsp;b = 10&nbsp;c=-40</p>    45  0
    15  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span>&nbsp;when a =10&nbsp;b = 10&nbsp;c=-40</p>    43  0
    9   <p>Physics Equations 1&nbsp;<span class="math-tex">\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span></p>  Easy 4  0
    15  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span>&nbsp;when a =10&nbsp;b = 10&nbsp;c=-40</p>    54  Mathematics
    9   <p>Physics Equations 1&nbsp;<span class="math-tex">\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span></p>  Easy 1  Medium
    5   <p>This is another Question with Demo data</p>  Junaid Rasheed  Easy
    2   <p>Testing&nbsp;<span class="math-tex">\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span></p>  100 0
    17  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span>&nbsp;when a =50&nbsp;b = 50&nbsp;c=-40</p>    435 0
    16  <p>Find values of x from&nbsp;<span class="math-tex">\(x = a^2 + b^2 - c\)</span>&nbsp;when a =20&nbsp;b = 20&nbsp;c=-40</p>    457 Hard
    8   <p>A hard &nbsp;question for English</p>    Hard 3  Medium

【问题讨论】:

  • 期望的输出是什么?
  • 您的第二级 IF 没有做任何有用的事情,因为它在两种情况下都返回 m_value
  • @Dai 这正是我显示错误输出的原因。因为如果我把它留空,它会显示一个 sql 错误
  • @Dekel 问题已更新

标签: mysql if-statement stored-procedures


【解决方案1】:

如果您只关心问题 ID。这是一种方式。

SELECT Question_ID
FROM Meta_values V
INNER JOIN Meta_Table T
 on V.m_ID = T.ID
WHERE m_value in ('Easy','English')
GROUP BY Question_ID
HAVING count(*)=2

注意 m_value 和 =2 将动态传递,只要您始终知道要查找的 m_value 的数量,您将 2 设置为该值。

如果您需要更多数据,请将其用作存在或内联视图连接,以限制仅匹配此结果集的问题。

【讨论】:

  • 当条件“Easy”或“English”中的任何一个为真时返回该行......但我想同时检查。
  • 这将返回 2 行。 ID 的 1 和 4. 在您​​的示例数据中。由于 ID 的 1 和 4 有问题 # 的 1 和 2,因此 count(*) 将为 1 而不是 2,并且不会返回任何记录。但是,如果我们查找“Easy”和“Physics”,则将返回第 1 行和第 2 行,因为它们都与问题 1 相关联,那么计数将为 2,并且将返回问题 1。
  • 你是真正的救星先生,非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-22
  • 1970-01-01
  • 2014-12-26
  • 2016-12-10
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
相关资源
最近更新 更多