简体   繁体   中英

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. 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.

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).

Have you tried using LEFT JOIN or RIGHT JOIN? They will do the trick since they do bring the NULL columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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