gpt4 book ai didi

mysql - 当 IF 为真时选择列

转载 作者:行者123 更新时间:2023-11-30 22:07:06 25 4
gpt4 key购买 nike

我有一个这样的场景..

元值表

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/

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