我没有深入研究您的代码,但也许这会有所帮助。
让我们从简化的数据库结构开始,如下所示:
CREATE TABLE `blocks` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` INT UNSIGNED NOT NULL,
`block_id` INT UNSIGNED NOT NULL );
INSERT INTO `blocks` (`user_id`,`block_id`) VALUES
(1,2),(3,4),(2,1);
CREATE TABLE `messages` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`author_id` BIGINT NOT NULL,
`message` TEXT NOT NULL );
INSERT INTO `messages` (`author_id`,`message`) VALUES
(1,"Message from user #1, who has a mutual block in place with user #2"),
(2,"Message from user #2, who has a mutual block in place with user #1"),
(3,"Message from user #3, who has blocked user #4"),
(4,"Message from user #4, who has been blocked by user #3"),
(5,"Message from user #5, who takes no part in all this blocking business");
现在让我们假设用户$n
访问了网站(其中 1≤<code>$n≤5)。要确定可以显示哪些消息,我们需要执行messages
和blocks
表的左连接 - 即,我们要考虑包含相关信息的每一行以及包含相关信息messages
的任何行(具体来说,消息的作者已阻止blocks
用户$n
,或已被用户屏蔽$n
)。如果$n
=1,我们有以下内容:
SELECT * FROM `messages`
LEFT JOIN `blocks`
ON (`author_id`=`block_id` AND `user_id`=1)
OR (`author_id`=`user_id` AND `block_id`=1);
这是该查询的结果:
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
| id | author_id | message | id | user_id | block_id |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
| 1 | 1 | Message from user #1, who has a mutual block in place with user #2 | NULL | NULL | NULL |
| 2 | 2 | Message from user #2, who has a mutual block in place with user #1 | 1 | 1 | 2 |
| 2 | 2 | Message from user #2, who has a mutual block in place with user #1 | 3 | 2 | 1 |
| 3 | 3 | Message from user #3, who has blocked user #4 | NULL | NULL | NULL |
| 4 | 4 | Message from user #4, who has been blocked by user #3 | NULL | NULL | NULL |
| 5 | 5 | Message from user #5, who takes no part in all this blocking business | NULL | NULL | NULL |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
6 rows in set (0.00 sec)
如您所见,我们想要的行是最后三列为 NULL 的行,这意味着没有阻止规则影响向该特定用户显示此特定消息。因此,要提取这些消息,我们只需在查询末尾添加:WHERE block_id
IS NULL
SELECT * FROM `messages`
LEFT JOIN `blocks`
ON (`author_id`=`block_id` AND `user_id`=1)
OR (`author_id`=`user_id` AND `block_id`=1)
WHERE `block_id` IS NULL;
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
| id | author_id | message | id | user_id | block_id |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
| 1 | 1 | Message from user #1, who has a mutual block in place with user #2 | NULL | NULL | NULL |
| 3 | 3 | Message from user #3, who has blocked user #4 | NULL | NULL | NULL |
| 4 | 4 | Message from user #4, who has been blocked by user #3 | NULL | NULL | NULL |
| 5 | 5 | Message from user #5, who takes no part in all this blocking business | NULL | NULL | NULL |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
4 rows in set (0.01 sec)
如果您将不同的用户 ID 替换到此查询中,您应该会得到您想要的结果。