mysql - 当 IF 为真时选择列

标签 mysql if-statement stored-procedures

我有一个这样的场景..

元值表

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

最佳答案

如果您只需要问题 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_values 的数量并将 2 设置为该值即可。

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

关于mysql - 当 IF 为真时选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41304379/

相关文章:

php - 获取特定日期不在另一个表中的记录。 MySQL

PHP 代码将空记录插入 MySQL 数据库

c - 尝试在 do while 循环中嵌套 if 语句时出现未知语法错误

无法进入 "if"条件。为什么?

mysql - 如何使用 PROCEDURE 在 MySQL 上运行多个查询?

MySQL:如何从触发器内的值创建集合?

entity-framework - 使用 Context.Database.SqlQuery<T> 和复杂类型映射执行存储过程

mysql - 如何在 MySQL 或 Excel 中将日期作为列标题使用 countif/Sumif

php - 更改mysql的默认日期

javascript - 如何优化 3 个条件 if else if 语句