gpt4 book ai didi

mysql - 如果答案为假,请选择正确的奇数

转载 作者:行者123 更新时间:2023-11-29 09:53:31 24 4
gpt4 key购买 nike

我的数据库中有 3 个表,名为:questionsanswerquestionodds

第一个(问题)有一个问题列表

+=========================+
| ID | question |
+=========================+
| 1 | Bla Bla |
| 2 | question |
| 3 | Other Question |
+-------------------------+

第二个(AnswerQuestion)有特定问题的用户答案

+==================================================+
| ID | idQuestion | idOdd | idUser | isCorrect |
+==================================================+
| 1 | 4 | 2 | 5 | 0 |
| 2 | 3 | 1 | 1 | 1 |
| 3 | 1 | 3 | 10 | 0 |
+--------------------------------------------------+

第三个(赔率)有正确赔率的问题

+============================================+
| ID | odd | idQuestion | isCorrect |
+============================================+
| 1 | One | 1 | 0 |
| 2 | Two | 1 | 1 |
| 3 | Three | 1 | 0 |
+--------------------------------------------+

因此,为了获取特定用户的答案,我使用此查询:

SELECT * FROM answerquestion a, question q, odds o
WHERE a.IdUser = :id AND a.IdOdd = o.ID AND a.IdQuestion = q.ID

工作正常,但如果用户的答案为假,我想得到正确的奇数为此,我在 SQL 上使用 IF 语句,查询是:

SET SQL_BIG_SELECTS=1;
SELECT DISTINCT IF(o.isCorrect = 0, o.Corr.TheCorr, o.odd), q.Question
FROM answerquestion a, odds o, question q,
( SELECT o.odd AS TheCorr FROM odds o, question q WHERE o.isCorrect = 1 AND o.IdQuestion = q.ID) AS Corr
WHERE a.IdUser = 5
AND q.ID = a.idQuestion
AND a.IdOdd = o.ID

此查询的问题在于答案是错误的,它给了我所有问题的所有正确答案并重复了问题

示例:

Question ID 4 'Bla Bla' | Correct odd of Question ID 1
Question ID 4 'Bla Bla' | Correct odd of Question ID 2
Question ID 4 'Bla Bla' | Correct odd of Question ID 3
Question ID 4 'Bla Bla' | Correct odd of Question ID 4
Question ID 4 'Bla Bla' | Correct odd of Question ID 5

我想要如果问题 ID 4 的答案,我需要得到它的正确奇数

Question ID 4 'Bla Bla' | Correct odd of Question ID 4

有什么帮助吗?

最佳答案

需要使用正确的 JOIN。不确定应该选择哪些列,但这很容易修复。

SELECT q.id, q.question, o.odd as correct, o2.odd as user_odd
FROM question q
JOIN AnswerQuestion a ON a.idQuestion = q.id
JOIN Odds o ON o.questionID = q.id AND o.isCorrect = 1
JOIN Odds o2 ON o2.id = a.idOdd
WHERE a.idUser = 5

关于mysql - 如果答案为假,请选择正确的奇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54328665/

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