Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在SELECT Case语句中合并行_Mysql_Sql_Join_Case - Fatal编程技术网

Mysql 在SELECT Case语句中合并行

Mysql 在SELECT Case语句中合并行,mysql,sql,join,case,Mysql,Sql,Join,Case,我有这个疑问 SELECT questions.question_id, questions.question, questions.answer_id, nlp_terms.word, ( CASE WHEN answers.id = questions.answer_id THEN answers.answer END ) AS answer, ( CASE

我有这个疑问

SELECT questions.question_id, 
       questions.question, 
       questions.answer_id, 
       nlp_terms.word,
       ( CASE 
           WHEN answers.id = questions.answer_id THEN answers.answer 
         END ) AS answer, 
       ( CASE 
           WHEN answers.id != questions.answer_id THEN answers.answer 
         END ) AS incorrect_answer_1, 
       ( CASE 
           WHEN answers.id != questions.answer_id THEN answers.answer 
         END ) AS incorrect_answer_2 
FROM   questions 
       JOIN answers 
         ON answers.question_id = questions.question_id 
       JOIN nlp_terms 
         ON questions.question_id = nlp_terms.question_id
WHERE questions.question_id = '1'
此查询输出:

╔═════════════╦═══════════════════════════════════════════╦═══════════╦═══════════╦════════╦════════════════════╦════════════════════╗
║ question_id ║                 question                  ║ answer_id ║   word    ║ answer ║ incorrect_answer_1 ║ incorrect_answer_2 ║
╠═════════════╬═══════════════════════════════════════════╬═══════════╬═══════════╬════════╬════════════════════╬════════════════════╣
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ NULL   ║ Australia          ║ AustraliA          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ NULL   ║ Australia          ║ Australia          ║
╚═════════════╩═══════════════════════════════════════════╩═══════════╩═══════════╩════════╩════════════════════╩════════════════════╝
正如您所看到的,它带来了我想要的所有数据,只是我的CASE语句采用了它们自己的一行,而不是分组

我进行了大量的
分组操作,但无法使其与我的预期输出相同:

╔═════════════╦═══════════════════════════════════════════╦═══════════╦═══════════╦════════╦════════════════════╦════════════════════╗
║ question_id ║                 question                  ║ answer_id ║   word    ║ answer ║ incorrect_answer_1 ║ incorrect_answer_2 ║
╠═════════════╬═══════════════════════════════════════════╬═══════════╬═══════════╬════════╬════════════════════╬════════════════════╣
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ Africa ║ South America      ║ Australia          ║
╚═════════════╩═══════════════════════════════════════════╩═══════════╩═══════════╩════════╩════════════════════╩════════════════════╝

要获得预期的输出,我缺少什么?

我认为这会产生您想要的结果:

SELECT q.question_id, q.question, q.answer_id, t.word,
       MAX(CASE WHEN a.id = q.answer_id THEN a.answer 
           END) AS answer, 
       MAX(CASE WHEN a.id <> q.answer_id THEN a.answer 
           END) AS incorrect_answer_1, 
       MIN(CASE WHEN a.id <> q.answer_id THEN a.answer 
           END) AS incorrect_answer_2 
FROM questions q JOIN
     answers a
     ON a.question_id = q.question_id JOIN
     nlp_terms t
     ON q.question_id = t.question_id
WHERE q.question_id = 1
GROUP BY q.question_id, q.question, q.answer_id, t.word;
选择q.question\u id、q.question、q.answer\u id、t.word、,
最大值(a.id=q.answer\u id然后a.answer时的情况)
(完)作为回答,,
最大值(a.id q.answer\u id然后a.answer时的情况)
结束)由于回答不正确,
最小值(a.id q.answer\u id然后a.answer时的情况
结束)因为回答不正确
从问题q连接
回答a
关于a.question\u id=q.question\u id连接
nlp_术语
关于q.question\u id=t.question\u id
其中q.question_id=1
按q.question\u id、q.question、q.answer\u id、t.word分组;
您是否尝试过“WHERE questions.question\u id='1'且答案不为空”


参考资料:

您所有的问题都只有3个答案吗?现在,是的,所有问题都有3个答案。结果相同。谢谢,是的!我爱你,谢谢。进行编辑
MIN(q.id
时的情况应为
MIN(q.question\u id
时的情况,假设我必须引入
nlp\u terms.id
自动增量(
t.id
)为了确保单词的顺序也正确?当我添加它时效果很好,因为上面的答案查询是按
t排序的。word
alphalcomcally你实际上在我面前发现了它(我的意思是不奇怪:),但我没有重新阅读整个更新以注意到)-查看更多关于它的
MIN(我之前评论过的CASE
完全不应该引用表q,事实上,在所有的
CASE
语句中都应该是
a.id
q.question\u id
。正如上面提到的那样,包含
GROUP BY
t.id
并以
顺序
BY
t.id
结束查询tput工作完美。再次感谢您分享您在这方面的知识和见解。我正在变得更好,您在我记事之前帮助了我。