如下所示,我正在检查当前用户是否在table friend的user_a或user_b列中.
根据当前用户所在的位置,我想得到他相应的朋友.
不知怎的,我不能让这个语法工作,并想知道是否有人可以告诉我什么是错的(我在IF user_a = 2附近的第3行得到一个错误.
SELECT *
FROM friends
IF user_a = 2 THEN
JOIN user_profiles ON friends.user_b = user.profiles.user_id
WHERE user_a = 2
AND accepted = 1;
ELSEIF user_b = 2 THEN
JOIN user_profiles ON friends.user_a = user_profiles.user_id
WHERE user_b = 2
AND accepted = 1;
END IF;
最佳答案
您可以使用UNION执行此操作:
原文链接:https://www.f2er.com/mysql/433599.htmlselect f.*,up_a.* from friends f
inner join user_profiles up_a on f.user_a=up_a.user_id
where f.user_b=2 and f.accepted=1
union
select f.*,up_b.* from friends f
inner join user_profiles up_b on f.user_b=up_b.user_id
where f.user_a=2 and f.accepted=1;