左连接可能是您要寻找的。
SELECT persons.id, persons.name, voters.personID IS NOT NULL AS hasVoted
FROM persons LEFT JOIN voters ON (persons.id = voters.personID)
WHERE persons.course = '1' AND voters.type = '2'
GROUP BY persons.id
http://dev.mysql.com/doc/refman/5.0/en/join.html
左连接将包括连接左侧的所有匹配行,以及连接右侧的任何匹配片段。左侧没有匹配右侧的任何行的右侧列值为 null。
至于使用 'IS NOT NULL' 或 'IS NULL' 是因为 'X = NULL' 的行为不像它出现的那样。
> SELECT 'A' = NULL, 'A' IS NULL, NULL = NULL, NULL IS NULL;
+------------+-------------+-------------+--------------+
| 'A' = NULL | 'A' IS NULL | NULL = NULL | NULL IS NULL |
+------------+-------------+-------------+--------------+
| NULL | 0 | NULL | 1 |
+------------+-------------+-------------+--------------+
有关使用空值的更多详细信息:http:
//dev.mysql.com/doc/refman/5.0/en/working-with-null.html