在一个查询中加入/查看Mysql三个表

时间:2013-07-01 07:47:15

标签: php mysql

我决定创建人们你可能知道的系统,但我已经解决了一个问题,我似乎需要加入三个表来带来相关的结果。

目的是在数据库表中检查可能的朋友,我可能根据朋友的朋友(if user1 = user2 and user2 = user4 posibile user1 knows user4, user4 is a suggestion)知道我已经使用以下代码检查这个并且正在计划中。

SELECT `friendpin` AS `possible_friend_id`
FROM `pinme`
WHERE `senderpin`
IN (
SELECT `friendpin`
FROM `pinme`
WHERE `senderpin` = $userlogged
)
AND `friendpin` NOT
IN (

SELECT `friendpin`
FROM `pinme`
WHERE `senderpin` = $userlogged
)
AND NOT `friendpin` = $userlogged
GROUP BY `possible_friend_id`
ORDER BY COUNT( * ) DESC
LIMIT 0 , 3 

因此可能是REQUEST表中的建议人员状态他/她已经有待处理的请求但仍然显示为建议的朋友,或者在BLOCKED表中建议的朋友被阻止但是仍然在你看来是建议的朋友。因此我想消除这种行为。建议的人不应该在REQUEST表或BLOCKED表中退出。

任何人都可以帮我解决这个问题吗?

以下是REQUEST表,PINME表和BLOCKED表结构

REQUEST TABLE:
senderpin - id of the person who send request   
friendpin - receivers request id    


BLOCKED TABLE: 
user1_id - a person who block user2_id
user2_id - the blocked person by user1_id


PINME TABLE:
PINME table: the table which keep relations
structure: 1,2 2,2 | 4,8 8,4
senderpin - id of the person who send request   
friendpin - receivers request id

谢谢和问候

1 个答案:

答案 0 :(得分:0)

你似乎需要做的是将pinme表连接到自身,然后再次反对自己找到匹配。对阻塞表和请求表执行LEFT OUTER JOIN以及结果,以排除已存在阻塞或请求记录的任何记录。

这样的事情: -

SELECT DISTINCT c.aPerson
FROM (SELECT friendpin AS aFriend FROM pinme WHERE senderpin = $userlogged UNION SELECT senderpin AS aFriend FROM pinme WHERE friendpin = $userlogged) a
INNER JOIN (SELECT senderpin AS aPerson, friendpin AS aFriend FROM pinme UNION SELECT senderpin AS aFriend, friendpin AS aPerson FROM pinme) b
ON a.aFriend = b.aFriend
INNER JOIN (SELECT senderpin AS aPerson, friendpin AS aFriend FROM pinme UNION SELECT senderpin AS aFriend, friendpin AS aPerson FROM pinme) c
ON b.aPerson = c.aFriend
LEFT OUTER JOIN (SELECT user1_id AS blocked FROM blocked WHERE user2_id = $userlogged UNION SELECT user2_id AS aFriend FROM blocked WHERE user1_id = $userlogged) d
ON c.aPerson = d.blocked
LEFT OUTER JOIN (SELECT senderpin AS blocked FROM request WHERE friendpin = $userlogged UNION SELECT friendpin AS aFriend FROM request WHERE senderpin = $userlogged) e
ON c.aPerson = e.blocked
WHERE d.blocked IS NULL
AND  e.blocked IS NULL
AND c.aPerson != $userlogged

编辑 - 修改建议

SELECT DISTINCT c.aPerson
FROM (SELECT senderpin AS aPerson, friendpin AS aFriend FROM pinme WHERE senderpin = $userlogged UNION SELECT senderpin AS aFriend, friendpin AS aPerson FROM pinme WHERE friendpin = $userlogged) a -- Find friends
INNER JOIN (SELECT senderpin AS aPerson, friendpin AS aFriend FROM pinme UNION SELECT senderpin AS aFriend, friendpin AS aPerson FROM pinme) b -- Find friends of friends who are not the original person
ON a.aFriend = b.aFriend AND a.aPerson != b.aFriend
LEFT OUTER JOIN (SELECT senderpin AS aPerson, friendpin AS aFriend FROM pinme WHERE senderpin = $userlogged UNION SELECT senderpin AS aFriend, friendpin AS aPerson FROM pinme WHERE friendpin = $userlogged) c -- Find friends
ON c.aFriend = b.aPerson 
LEFT OUTER JOIN (SELECT user1_id AS blocked FROM blocked WHERE user2_id = $userlogged UNION SELECT user2_id AS aFriend FROM blocked WHERE user1_id = $userlogged) d
ON b.aPerson = d.blocked
LEFT OUTER JOIN (SELECT senderpin AS blocked FROM request WHERE friendpin = $userlogged UNION SELECT friendpin AS aFriend FROM request WHERE senderpin = $userlogged) e
ON b.aPerson = e.blocked
WHERE d.blocked IS NULL
AND  e.blocked IS NULL
AND  c.aFriend IS NULL
AND b.aPerson != $userlogged