1

历史:

一个事件有很多参与者。参与者都在活动中相遇,并向他们真正喜欢的所有其他参与者发出“赞”。

在活动结束时,管理员为该活动的每个参与者插入所有喜欢,系统将找到相互喜欢(友谊)

问题:在插入喜欢(双关语)系统以检测天气时,已经建立了友谊(也来自其他事件),如果是这样,请避免在设置喜欢时显示该用户名。

这是我正在使用的表(mysql)

wp_fd_users

id | user_name | user_gender | ... etc
1  | x         | 0           | ...
2  | y         | 0           | ...
3  | z         | 1           | ...
4  | q         | 1           | ...

wp_fd_subscriptions

id | event_id | event_user_id | ...etc
1  | 1        | 1             | ...etc
2  | 1        | 2             | ...etc
3  | 1        | 3             | ...etc
4  | 1        | 4             | ...etc

wp_fd_matches

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;

这将返回订阅表中与用户表和事件表连接的所有记录。具体来说,我需要得到的不是已经参加过给定活动的给定用户的共同朋友

上面写的 sql 现在总是给我同一行。

似乎避免了与任何其他用户有关系的任何用户

此外,如果我在查询中更改用户性别,我总是得到零结果。

编辑:很难准确定义需要什么,但希望我能到达那里:查询必须返回所有用户: 1. 参与事件 1,即使他们在 wp_fd_matches 表中没有任何条目 2. 不是重点用户 3. 无论如何都与重点用户没有相互关系

其他讨论的链接在这里:mutual non-mutual friend query mysql

4

2 回答 2

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;
于 2012-08-21T10:05:06.627 回答
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;

编辑::

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;
于 2012-08-21T07:55:37.107 回答