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