MySQL 来自第三个 table 的复杂 select 查询过滤
MySQL complex select query filtering from third table
我有三个 table。
users, friends and communityusers
table秒。
我正在尝试构建一个类似于 facebook 的群组系统。用户将尝试从好友列表中搜索 his/her 位好友以将他们添加到组中。
所以我在这里试图删除那些已经添加到组中的朋友。这里组用户 table 是 communityusers
..
这是我的communityusers
table的结构。
id | community_id | user_id
我无法在当前查询中使用此 table。
SELECT f.id, u.id as user_id, u.userName, u.firstName, u.lastName, u.profilePic from friends f, users u
WHERE CASE
WHEN f.following_id=1
THEN f.follower_id = u.id
WHEN f.follower_id=1
THEN f.following_id = u.id
END
AND
f.status= 2
AND
(
u.firstName LIKE 's%' OR u.lastName LIKE 's%'
)
此查询 returns 用户 ID 1
的好友列表现在我想排除从 communityusers
table 过滤的用户。
此查询中的 user_id
不应出现在 communityusers
table 中。我如何从 communityusers
或第三个 table 中过滤这个?
如果问题不清楚,请告诉我。
任何建议、想法和帮助将不胜感激。
非常感谢。
编辑
在 运行 我得到的查询之后
我的communityusers
table
看到user_id = 2
也是从上面的查询中选出来的。我想从结果中删除 user_id = 2
因为它存在于 communityusers
table.
我正在尝试创建一个 Fiddle 但由于某些原因它对我不起作用。
再次感谢。
尝试在给定条件下使用 communityuser table 左连接 cu.user_id 为 null - 这将为您提供那些不在 communituuser table
中的用户
select a.* from
(SELECT f.id, u.id as user_id, u.userName, u.firstName, u.lastName, u.profilePic from friends f inner join users u
on CASE WHEN f.following_id=1 THEN f.follower_id = u.id WHEN f.follower_id=1 THEN f.following_id = u.id
END AND f.status= 2 where u.firstName LIKE 's%' OR u.lastName LIKE 's%')a
left join (select * from communityuser where community_id<>4) cu on a.user_id=cu.user_id
where cu.user_id is null
@sadek
只需使用 not in, in where 条件
SELECT f.id, u.id as user_id, u.userName, u.firstName, u.lastName, u.profilePic
from friends f inner join users u on
CASE WHEN f.following_id=1 THEN f.follower_id = u.id WHEN f.follower_id=1 THEN f.following_id = u.id
END AND f.status= 2
where u.id not in (select distinct user_id from communityusers) and (u.firstName LIKE 's%' OR u.lastName LIKE 's%')
我有三个 table。
users, friends and communityusers
table秒。
我正在尝试构建一个类似于 facebook 的群组系统。用户将尝试从好友列表中搜索 his/her 位好友以将他们添加到组中。
所以我在这里试图删除那些已经添加到组中的朋友。这里组用户 table 是 communityusers
..
这是我的communityusers
table的结构。
id | community_id | user_id
我无法在当前查询中使用此 table。
SELECT f.id, u.id as user_id, u.userName, u.firstName, u.lastName, u.profilePic from friends f, users u
WHERE CASE
WHEN f.following_id=1
THEN f.follower_id = u.id
WHEN f.follower_id=1
THEN f.following_id = u.id
END
AND
f.status= 2
AND
(
u.firstName LIKE 's%' OR u.lastName LIKE 's%'
)
此查询 returns 用户 ID 1
的好友列表现在我想排除从 communityusers
table 过滤的用户。
此查询中的 user_id
不应出现在 communityusers
table 中。我如何从 communityusers
或第三个 table 中过滤这个?
如果问题不清楚,请告诉我。
任何建议、想法和帮助将不胜感激。 非常感谢。
编辑
在 运行 我得到的查询之后
我的communityusers
table
看到user_id = 2
也是从上面的查询中选出来的。我想从结果中删除 user_id = 2
因为它存在于 communityusers
table.
我正在尝试创建一个 Fiddle 但由于某些原因它对我不起作用。
再次感谢。
尝试在给定条件下使用 communityuser table 左连接 cu.user_id 为 null - 这将为您提供那些不在 communituuser table
中的用户select a.* from
(SELECT f.id, u.id as user_id, u.userName, u.firstName, u.lastName, u.profilePic from friends f inner join users u
on CASE WHEN f.following_id=1 THEN f.follower_id = u.id WHEN f.follower_id=1 THEN f.following_id = u.id
END AND f.status= 2 where u.firstName LIKE 's%' OR u.lastName LIKE 's%')a
left join (select * from communityuser where community_id<>4) cu on a.user_id=cu.user_id
where cu.user_id is null
@sadek 只需使用 not in, in where 条件
SELECT f.id, u.id as user_id, u.userName, u.firstName, u.lastName, u.profilePic
from friends f inner join users u on
CASE WHEN f.following_id=1 THEN f.follower_id = u.id WHEN f.follower_id=1 THEN f.following_id = u.id
END AND f.status= 2
where u.id not in (select distinct user_id from communityusers) and (u.firstName LIKE 's%' OR u.lastName LIKE 's%')