MySQL左连接,其中返回NULL
正在尝试加入几个表,以向用户提供他订阅此会话的其他用户的一些信息。 有一个其他用户及其图片的列表 这项工作:MySQL左连接,其中返回NULL,mysql,Mysql,正在尝试加入几个表,以向用户提供他订阅此会话的其他用户的一些信息。 有一个其他用户及其图片的列表 这项工作: SELECT DISTINCT profile_picupload.imageFull, goals_list.goal, goal03.other, users.username, users.email FROM profile_picupload JOIN anal_muses ON anal_muses.username = profile_picupload.usern
SELECT DISTINCT profile_picupload.imageFull, goals_list.goal, goal03.other,
users.username, users.email FROM profile_picupload
JOIN anal_muses
ON anal_muses.username = profile_picupload.username
JOIN goal03 ON goal03.username = anal_muses.username
JOIN goals_list ON goal03.goal=goals_list.id
JOIN users ON users.email = anal_muses.username
它给了我想要的清单
但是-我只需要anal_muses提供的本次会议的列表-因此我将代码更改为:
SELECT DISTINCT profile_picupload.imageFull, goals_list.goal, goal03.other,
users.username, users.email FROM profile_picupload
right JOIN anal_muses
ON anal_muses.username = profile_picupload.username
AND anal_muses.sessionId='$sessionId'
JOIN goal03 ON goal03.username = anal_muses.username
JOIN goals_list ON goal03.goal=goals_list.id
JOIN users ON users.email = anal_muses.username
表profile_picupload-每个用户只保存一张图片
而表anal_musers为每个用户保存许多行(取决于他订阅的人数)
因此,当我添加和anal_muses.sessionId='$sessionId'时,我需要将第一个连接更改为右连接
但是现在我得到了一个profile_picupload.imageFull的空值列表
有办法解决这个问题吗
谢谢:-)
右连接
意味着它应该从分析
返回在profile\u picupload
中没有任何匹配项的行,并将NULL
放在这些列中。这就是JOIN
和RIGHT JOIN
之间的区别。所以问题是。。你为什么需要“外部”连接?我会得到profile_picupload.imageFull,直到我添加:和anal_muses.sessionId='$sessionId'..@Barmar-谢谢-查看我之前的评论。仅当我添加额外的和时,我的某些内容(Distinct为我提供了出于某种原因需要的完整数据…)才存在Null。。。。。没有imageFull我也有同样的结果fiels@xQbert:-)-我不知道你说的“外部”是什么意思:-。其他连接是为了获得更多相关数据。当我在没有它们的情况下运行查询时,问题仍然存在。。没有:和anal_muses,一切都很好。sessionId='$sessionId'