我正在使用SQL制作朋友网络功能。我使用了以下代码:
SELECT relationship.requester,
relationship.requested,
user.firstname,
user.lastname
FROM relationship
JOIN user ON (user.guid = relationship.requester
OR user.guid = sqrelationship.requested)
WHERE relationship.requester = {$userid}
OR relationship.requested = {$userid}
AND relationship.approved = 1
SQL将返回一个表,其中包含用户发起或接收的已批准关系的ID。但是,user.firstname和user.lastname只返回一组结果。如何使这个表达式能够同时获取请求者和被请求者的名字?
答案 0 :(得分:1)
很难说明你没有指定表格设计的问题,但我猜是这样的:
select relationship.requester, relationship.requested,
requester.firstname, requester.lastname,
requested.firstname, requested.lastname,
from relationship
left join user as requester on user.guid = relationship.requester
left join user as requested on user.guid = relationship.requested
where relationship.requester = {$userid}
and relationship.approved = 1