简体   繁体   English

为什么当我希望MySQL查询全部显示时,它会省略记录?

[英]Why does MySQL query omit records when I want it to show them all?

I'm trying to output all the records in my candidate table using an SQL query. 我正在尝试使用SQL查询输出我的候选表中的所有记录。 It outputted all the records fine until I added INNER JOINs to also output note data from a table of notes which is linked by the noteID. 它很好地输出了所有记录,直到我添加了INNER JOINs也从由noteID链接的注释表中输出注释数据。

It now only outputs records where there are notes attached... but I want all records outputted with or without notes. 现在,它仅输出附有注释的记录...,但我希望输出包含或不包含注释的所有记录。

try {
    $sql   = 'SELECT candidate.Firstname, candidate.Lastname, candID, candidate.Email,
              date, userID, note, noteID, username
    FROM candidate INNER JOIN note ON LastNoteID=noteID INNER JOIN user ON userID=id';
    $result = $pdo->query($sql);
}
catch (PDOException $e) {
    $error = 'Error fetching candidates: ' . $e->getMessage();
    include $errorpage;
    exit();
}
while ($row = $result->fetch()) {
    $cands[] = array(
        'id' => $row['candID'],
        'firstname' => $row['Firstname'],
        'lastname' => $row['Lastname'],
        'email' => $row['Email'],
        'noteusername' => $row['username'],
        'notedate' => $row['date'],

    );
} 

How can I fix this so it shows all records in the candidate table with or without notes? 如何解决此问题,使其显示候选表中的所有记录(带或不带注释)?

INNER JOIN does not include rows which have got NULL columns (like you are requesting). INNER JOIN不包括具有NULL列的行(如您所请求的)。

Have you tried using LEFT JOIN or RIGHT JOIN? 您是否尝试过使用LEFT JOIN或RIGHT JOIN? They will do the trick since they do bring the NULL columns. 他们会做到这一点,因为它们确实带来了NULL列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM