我正在尝试使用 SQL 查询输出候选表中的所有记录。它可以很好地输出所有记录,直到我添加 INNER JOIN 以从由 noteID 链接的注释表中输出注释数据。
它现在只输出附有注释的记录...但我希望所有记录都输出有或没有注释。
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'],
);
}
我怎样才能解决这个问题,让它显示候选表中的所有记录,有或没有注释?
最佳答案
INNER JOIN 不包括具有 NULL 列的行(如您所请求的那样)。
您是否尝试过使用 LEFT JOIN 或 RIGHT JOIN?他们会做到这一点,因为他们确实带来了 NULL 列。
关于php - 为什么 MySQL 查询在我想要显示所有记录时却忽略了记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26125830/