$sql = "select u.Id, u.username, u.email, (NOW()-u.authenticationTime) as authenticateTimeDifference, u.IP,
f.providerId, f.requestId, f.status, u.port
from friends f
left join users u on
u.Id = if ( f.providerId = ".$userId.", f.requestId, f.providerId )
where (f.providerId = ".$userId." and f.status=".USER_APPROVED.") or
f.requestId = ".$userId." ORDER BY u.username ASC";
我不明白这里的if含义,与where
一起使用// providerId and requestId is Id of a friend pair,
// providerId is the Id of making first friend request
// requestId is the Id of the friend approved the friend request made by providerId
// fetching friends,
// left join expression is a bit different,
// it is required to fetch the friend, not the users itself
谁能为我解释一下? thanx
答案 0 :(得分:2)
只有当条件f.providerId = ".$userId."
为真时,才能通过requestId在好友和用户之间建立联接。如果不是,则由providerId进行连接。
我看到$userId
是一个你传递给查询的php变量,所以可能的想法是检查活动用户是否是提供者,然后获取与请求相关联的行而不是朋友表中的提供者。
关于If函数的文档:http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_if