历史:
对于一个活动有很多参与者。 参与者在活动中彼此相遇,并向他们真正喜欢的所有其他参与者发出“喜欢”。
在活动结束时,管理员会为THAT事件的每个参与者插入所有喜欢的内容,系统会找到相互喜欢的(友情)
问题: 在插入喜欢的时候,我希望(双关)系统检测天气已经建立了友谊(也来自其他事件),如果是,则避免在设置喜欢时显示该用户名。
以下是我正在使用的表(mysql)
id | user_name | user_gender | ... etc
1 | x | 0 | ...
2 | y | 0 | ...
3 | z | 1 | ...
4 | q | 1 | ...
id | event_id | event_user_id | ...etc
1 | 1 | 1 | ...etc
2 | 1 | 2 | ...etc
3 | 1 | 3 | ...etc
4 | 1 | 4 | ...etc
id | event_id | event_user_id | event_user_match_id | ... etc
1 | 1 | 1 | 3 | ... etc
2 | 1 | 3 | 1 | ... etc
3 | 1 | 1 | 4 | ... etc
以下是Gordon Linoff亲切地给我回复NON MUTUAL Friends的查询
select m1.*
from wp_fd_matches m1 left outer join
wp_fd_matches m2
on m1.event_id = m2.event_id and
m1.event_user_id = m2.event_user_match_id
m1.event_user_match_id = m2.event_user_id
where m2.id is null
现在我需要将它集成到以下查询中(并稍微优化一下,以便此子查询不会返回所有记录,然后过滤它们) - 这里它已经插入:
SELECT *, wp_fd_users.id AS userid, wp_fd_users.user_gender AS usergender
FROM wp_fd_subscriptions JOIN wp_fd_events ON event_id = wp_fd_events.id
JOIN wp_fd_users ON event_user_id = wp_fd_users.id
WHERE (wp_fd_subscriptions.event_id = 1
AND wp_fd_users.id != 2 AND wp_fd_users.user_gender != 0
AND wp_fd_users.id IN (
select m1.user_event_match_id
from wp_fd_matches m1 left outer join
wp_fd_matches m2 on m1.event_id = m2.event_id and
m1.event_user_id = m2.event_user_match_id and
m1.event_user_match_id = m2.event_user_id where m2.id is null
)
)
ORDER BY wp_fd_users.user_name;
这将返回与user表和events表连接的订阅表中的所有记录。 具体而言,我需要获得的是并非已经参与特定活动的特定用户的共同朋友
上面写的sql让我总是在同一行。
似乎避免任何与任何其他用户有关系的用户
此外,如果我在查询中更改用户性别,我总是得到零结果。
编辑:很难准确定义所需内容,但希望我能够实现目标: 查询必须返回以下所有用户: 1.参与活动1,即使他们在wp_fd_matches表中没有任何条目 2.不是专注的用户 3.在任何情况下都不要与重点用户建立相互关系
其他讨论的链接在这里:mutual non-mutual friend query mysql
答案 0 :(得分:1)
event_id = 1,您的用户ID = 1
SELECT u.*
FROM wp_fd_subscriptions AS s
JOIN wp_fd_users AS u
ON s.event_user_id = u.id
LEFT JOIN wp_fd_matches m1
ON m1.event_id = s.event_id
AND m1.event_user_id = 1
AND m1.event_user_match_id = s.event_user_id
LEFT JOIN wp_fd_matches m2
ON m1.event_id = m2.event_id
AND m2.event_user_id = m1.event_user_match_id
AND m2.event_user_match_id = 1
WHERE s.event_id=1 AND u.id != 1 AND m2.id IS NULL;
答案 1 :(得分:0)
问题不明确。正如你所说的,userid4应该进入结果集
SELECT *, wp_fd_users.id AS userid, wp_fd_users.user_gender AS usergender
FROM wp_fd_subscriptions JOIN wp_fd_events ON event_id = wp_fd_events.id
JOIN wp_fd_users ON event_user_id = wp_fd_users.id
WHERE (wp_fd_subscriptions.event_id = 1
AND wp_fd_users.id != 2 AND wp_fd_users.user_gender != 0
AND wp_fd_users.id IN (
select m1.event_user_match_id
from wp_fd_matches m1 left outer join
wp_fd_matches m2 on m1.event_id = m2.event_id and
m1.event_user_id = m2.event_user_match_id and
m1.event_user_match_id = m2.event_user_id where m2.id is null
)
)
ORDER BY wp_fd_users.user_name;
EDIT ::
SELECT *, wp_fd_users.id AS userid, wp_fd_users.user_gender AS usergender
FROM wp_fd_subscriptions JOIN wp_fd_events ON event_id = wp_fd_events.id
JOIN wp_fd_users ON event_user_id = wp_fd_users.id
WHERE (wp_fd_subscriptions.event_id = 1
AND wp_fd_users.id != 2 AND wp_fd_users.user_gender != 0
AND( wp_fd_users.id IN (
select m1.user_event_match_id
from wp_fd_matches m1 left outer join
wp_fd_matches m2 on m1.event_id = m2.event_id and
m1.event_user_id = m2.event_user_match_id and
m1.event_user_match_id = m2.event_user_id where m2.id is null
) or NOT EXISTS (SELECT * FROM wp_fd_matches b
where wp_fd_users.id =b.user_event_match_id))
)
ORDER BY wp_fd_users.user_name;