Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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 如果没有得到回答,就去问问题_Mysql - Fatal编程技术网

Mysql 如果没有得到回答,就去问问题

Mysql 如果没有得到回答,就去问问题,mysql,Mysql,基本上,有三个表具有以下结构 第一张表格:所有问题都有 id tournament_id question active created_at updated_at 1 5 Text question 1 2018-12-08 20:28:49 NULL 第二张表:锦标赛选项,拥有所有选项 id question_id option correct active created_at

基本上,有三个表具有以下结构

第一张表格:所有问题都有

id tournament_id    question     active   created_at          updated_at
 1         5       Text question    1   2018-12-08 20:28:49     NULL
第二张表:锦标赛选项,拥有所有选项

id  question_id option  correct active  created_at           updated_at
1      1         1       1       1     2018-12-08 20:29:02     NULL
2      1        26       0       1     2018-12-08 20:29:02     NULL
第三个表:锦标赛用户答案,拥有所有用户答案

id  option_id   user_id score   active  created_at  updated_at 
此表目前没有数据

我想实现所有用户没有回答的问题,因此查询应该返回第一个问题。这是我尝试过的查询,但它总是返回null


也许这适用于您的案例简化版本:

SELECT q.id, q.question, ua.id ua_id FROM tournament_questions q
INNER JOIN tournament_options o ON q.id = o.question_id
LEFT JOIN tournament_user_answers ua ON o.id = ua.option_id
GROUP BY q.id
HAVING ua_id IS NULL
您可以从Tornament_user_answers开始,它是空的,然后执行一个左连接,该连接包括左侧为空的所有行,并在这些行上附加右侧的数据(如果可用)。空的左联接数据将为空

SELECT 
    tournament_questions.*
FROM tournament_questions
JOIN tournament_options 
    ON tournament_options.question_id = tournament_questions.id
    AND tournament_options.active = 1
LEFT JOIN tournament_user_answers 
    ON tournament_user_answers.option_id = tournament_options.id
    AND tournament_user_answers.user_id = 1
WHERE
    tournament_questions.tournament_id = 5 
    AND tournament_questions.active = 1 
GROUP BY tournament_questions.id
HAVING MAX(tournament_user_answers.id) IS NULL
ORDER BY tournament_questions.id ASC 

在这种情况下,左侧部分的问题+选项包含数据,如果可用,将附加答案。如果包含MaxTornament\u user\u answers.id,则您将得到所有没有答案的问题。

感谢Peter的输入,但查询不会给出结果。它不返回任何问题,但由于用户未提供任何答案,因此第一个问题应该在那里。感谢Peter提供的解决方案,为用户添加了活动列,这样做没有问题。谢谢只是一个简单的问题,我们是否可以使用上述相同的查询获得特定问题的所有选项,并将选项名称和选项id放置在两个新列中,并使用group coalesce或类似的方法用逗号分隔?是的,在您的选择部分中添加一个。例如,GROUP_concatornument_options.id作为optionIdSyp,也可以使用。也有机会以随机顺序获得选项。已经在使用ORDER BY rand并以随机顺序获取问题。感谢Steve的输入,但是即使已经回答了问题,查询也会返回问题。要求是在特定的比赛中获得特定用户没有回答的问题。
SELECT 
    tournament_questions.*
FROM tournament_questions
JOIN tournament_options 
    ON tournament_options.question_id = tournament_questions.id
    AND tournament_options.active = 1
LEFT JOIN tournament_user_answers 
    ON tournament_user_answers.option_id = tournament_options.id
    AND tournament_user_answers.user_id = 1
WHERE
    tournament_questions.tournament_id = 5 
    AND tournament_questions.active = 1 
GROUP BY tournament_questions.id
HAVING MAX(tournament_user_answers.id) IS NULL
ORDER BY tournament_questions.id ASC