Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的数据库中有一个名为 Users 的表。一个用户可以有多个地址,但在某些情况下他们不会有任何地址。我的 mysql 链接语句只返回具有至少一个地址的记录:
$query = ' SELECT DISTINCT a.id FROM users as a, addresses as b' ; $query.= ' WHERE a.id = b.userId ' ;
我怎样才能改变它,以便仍然返回没有地址的用户?
我猜有一个简单的解决方案,我只是想念!
提前致谢。
改用LEFT JOIN:
LEFT JOIN
SELECT DISTINCT a.id FROM Users a LEFT JOIN Addresses b on a.ID = b.UserID
您需要使用LEFT JOIN:
$query = ' SELECT DISTINCT a.id FROM users as a LEFT JOIN addresses as b' ; $query.= ' ON a.id = b.userId ' ;