PHP RIGHT JOIN查询不考虑null?

时间:2016-12-07 01:57:07

标签: php mysql right-join

当我运行查询时,它返回所有正确的值,除了它不返回任何a.Correct = null的值(即测试问题根本没有得到回答),但它确实返回a的值。正确= 0。

 /***Configure the retest***/
    public function CreateRetest($topicID) {
    $retestData = array();
    $count = 0;

    $createSQL = "SELECT q.QuestionID,q.Question,q.Answer,q.TopicID From quizanswered a RIGHT JOIN question q ON a.QuestionID = q.QuestionID where (a.Correct=null OR a.Correct = 0) AND q.TopicID = '$topicID'";
    $create = mysqli_query($this->db,$createSQL) or die(mysqli_connect_errno()."Cannot create tables");
    while($row = mysqli_fetch_array($create)){
    $rQuestionID = $row['QuestionID'];
    $rQuestion = $row['Question'];
    $rAnswer = $row['Answer'];
    $retestData[$count] = array($rQuestionID,$rQuestion,$rAnswer);
    $count +=1;
    }
    return $retestData;

 }

1 个答案:

答案 0 :(得分:0)

=null替换为IS NULL

SELECT 
    q.QuestionID,
    q.Question,
    q.Answer,
    q.TopicID 
FROM quizanswered a 
RIGHT JOIN question q 
ON a.QuestionID = q.QuestionID 
WHERE (a.Correct IS NULL OR a.Correct = 0) AND q.TopicID = '$topicID'