加入返回的零元素不应该

时间:2013-05-14 14:00:57

标签: mysql join

下面的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

编辑: 感谢您的回答和评论,但不幸的是,即使在尝试为null之后,它仍然返回零行,我认为我应该检查blockedusers.profile_id_2而是将友好表的第一个用户存在的两种可能性分开(在friendship.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

2 个答案:

答案 0 :(得分:1)

我认为这会做你想要的。最后一个连接的最后一行应该与友谊中的第二个ID匹配。如果匹配,则WHERE子句中的blockedusers.profile_id_1 IS 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   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 

答案 1 :(得分: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