我有下表:
朋友:
id | user1 | user2 | status
1 | Jorge | Alisson | 3
2 | Lucas | Jorge | 3
要执行
SELECT
,我执行(变量$userLogged
指当时登录的用户):SELECT user1, user2, status
FROM friends
WHERE user1 = '$userLogged' OR user2 = '$userLogged'
所以,在PHP中:
foreach($query as $q) {
if($q["user1"] == $userLogged) {
$userFriend = $q["user2"];
} else {
$userFriend = $q["user1"];
}
}
假设
$userLogged
是Jorge。我想找个朋友,也就是说,那不是(艾莉森和卢卡斯,以防万一)。目前,我用PHP按照上面所示的方式进行检查。如何在查询中直接执行此操作,从而在PHP中保存此签入?例如:$userFriend
。。。 最佳答案
SELECT
CASE WHEN user1 = '$userLogged' THEN user2
ELSE user1 END AS userFriend
FROM friends
WHERE user1 = '$userLogged' OR user2 = '$userLogged'