0

下面的 SQL 语句通过使用用户的 profile_id#s 查询友谊表来查找两个用户元素之间的友谊,但是我最近添加到工作语句中的行是LEFT OUTER JOIN block_user_filters AS blockedusers on blockedusers.profile_id_1 = 'abcde2'我想查看用户 abcde2 是否阻止了任何人,如果是的话我想要从 where 子句的表中过滤这些友谊,在该子句LEFT OUTER JOIN block_user_filters AS blockedusers on blockedusers.profile_id_1 = 'abcde2'中,我将块表填充了用户 abcde2 的块行到另一个友元用户元素,但是整个语句返回 0 行。如果可以的话,请帮我解决这个问题。谢谢

SELECT  
  users1.username AS firstusername, 
  users2.username AS secondusername,
  users1.profile_id AS firstprofid,
  users2.profile_id AS secondprofid,
  users1.picup AS firstpicup, 
  users2.picup AS secondpicup
FROM  `users` 
  LEFT OUTER JOIN  `friendship` 
        ON friendship.profile_id_1 = users.profile_id OR friendship.profile_id_2 = users.profile_id
  LEFT OUTER JOIN users AS users1 ON users1.profile_id = friendship.profile_id_1
  LEFT OUTER JOIN users AS users2 ON users2.profile_id = friendship.profile_id_2
  LEFT OUTER JOIN block_user_filters AS blockedusers on blockedusers.profile_id_1 = 'abcde2'
  WHERE users.profile_id = 'abcde2' and blockedusers.profile_id_1 != 'abcde2' and friendship.state = 1 limit 6

编辑:感谢您的回答和评论,但不幸的是,即使尝试为空,它仍然返回零行,我想我应该检查blockedusers.profile_id_2 并将友谊表的第一个用户存在的两种可能性分开(无论是友谊.profid_1 或 _2),并将它们与 UNION 结合起来,但这会产生更奇怪的结果,我只需要集中注意力......

SELECT users1.username AS firstusername, users.username AS secondusername, 
users1.profile_id AS firstprofid, users.profile_id AS secondprofid,
users1.picup AS firstpicup, users.picup AS secondpicup
FROM  `users` 
LEFT OUTER JOIN  `friendship` ON friendship.profile_id_2 = users.profile_id
LEFT OUTER JOIN users AS users1 ON users1.profile_id = friendship.profile_id_1
LEFT OUTER JOIN block_user_filters AS blockedusers on blockedusers.profile_id_1 = 'abcde2'
WHERE users.profile_id = 'abcde2' and blockedusers.profile_id_2 != friendship.profile_id_1 and friendship.state = 1
UNION
SELECT users.username AS firstusername, users.username AS secondusername, 
users.profile_id AS firstprofid, users2.profile_id AS secondprofid,
users.picup AS firstpicup, users2.picup AS secondpicup
FROM  `users` 
LEFT OUTER JOIN  `friendship` ON friendship.profile_id_1 = users.profile_id
LEFT OUTER JOIN users AS users2 ON users2.profile_id = friendship.profile_id_2
LEFT OUTER JOIN block_user_filters AS blockedusers on blockedusers.profile_id_1 = 'abcde2'
WHERE users.profile_id = 'abcde2' and blockedusers.profile_id_2 != friendship.profile_id_2 and friendship.state = 1
4

2 回答 2

1

我认为这将满足您的需求。最后加入的最后一行应该匹配友谊中的第二个 ID。如果有匹配,blockedusers.profile_id_1 IS NULLWHERE 子句中的条件将忽略它。

SELECT users1.username   AS firstusername, 
       users2.username   AS secondusername, 
       users1.profile_id AS firstprofid, 
       users2.profile_id AS secondprofid, 
       users1.picup      AS firstpicup, 
       users2.picup      AS secondpicup 

FROM   friendship 

       LEFT OUTER JOIN users AS users1 
                    ON users1.profile_id = friendship.profile_id_1 
       LEFT OUTER JOIN users AS users2 
                    ON users2.profile_id = friendship.profile_id_2 
       LEFT OUTER JOIN block_user_filters AS blockedusers 
                    ON  blockedusers.profile_id_1 = 'abcde2'
                    AND blockedusers.profile_id_2 IN (users1.profile_id, users2.profile_id)

WHERE  (friendship.profile_id_1 = 'abcde2' or friendship.profile_id_2 = 'abcde2')
       AND friendship.state = 1 
       AND blockedusers.profile_id_1 IS NULL 

LIMIT  6 
于 2013-05-17T16:09:07.973 回答
0

您需要更改 where 子句。 blockedusers.profile_id_1 != 'abcde2'正在过滤掉所有结果。因为如果blockedusers 表中有匹配项,则profile_id_1 的值为'abcde2'。如果没有匹配,该字段的返回值为 NULL,WHERE 子句也将失败。检查 profile_id_1 是否为 NULL 以获取未被阻止的朋友。

SELECT users1.username   AS firstusername, 
       users2.username   AS secondusername, 
       users1.profile_id AS firstprofid, 
       users2.profile_id AS secondprofid, 
       users1.picup      AS firstpicup, 
       users2.picup      AS secondpicup 
FROM   users 
       LEFT OUTER JOIN friendship 
                    ON friendship.profile_id_1 = users.profile_id 
                        OR friendship.profile_id_2 = users.profile_id 
       LEFT OUTER JOIN users AS users1 
                    ON users1.profile_id = friendship.profile_id_1 
       LEFT OUTER JOIN users AS users2 
                    ON users2.profile_id = friendship.profile_id_2 
       LEFT OUTER JOIN block_user_filters AS blockedusers 
                    ON blockedusers.profile_id_1 = 'abcde2' 
WHERE  users.profile_id = 'abcde2' 
       AND blockedusers.profile_id_1 IS NULL 
       AND friendship.state = 1 
LIMIT  6 
于 2013-05-14T14:11:04.743 回答