У меня есть этот запрос
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 вместо собственной группировки берут собственную строку.
У меня есть многочисленные GROUP BY
операции, но я не могу получить их так, как ожидалось:
╔═════════════╦═══════════════════════════════════════════╦═══════════╦═══════════╦════════╦════════════════════╦════════════════════╗
║ 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 ║
╚═════════════╩═══════════════════════════════════════════╩═══════════╩═══════════╩════════╩════════════════════╩════════════════════╝
Чего мне не хватает, чтобы получить ожидаемый результат?