我曾尝试构建一个 Block 函数,但没有任何运气,我的 SQL 技能在这种情况下不如我希望的那么好。
我有一张名为“消息”的表和一张名为“ block ”的表 现在,有 1 个文件将所有内容同步到聊天,我想做的是,如果用户 1 阻止了用户 2,那么用户 1 的消息永远不会到达用户 2,用户 2 的消息不应该到达用户 1。短期内,如果你屏蔽了一个你不能和他/她说话的人,他/她也不能和你说话!
"blocks" table:
id bigint(20)
user_id tinyint(20)
block_id tinyint(20)
"messages" table:
id bigint(20)
timestamp datetime
dest_type varchar(255)
dest_id bigint(20)
source_type varchar(255)
source_id bigint(20)
message_type varchar(255)
message text
在“ block ”中,user_id 是 block 行的所有者 ID。
block_id 是所有者想要阻止的 id。
IF "messages.source_id = blocks.block_id OR messages.block_id = blocks.user_id"
不要让消息传播出去。我知道让别人为我编写代码是很粗鲁的,但我想问一下,有人可以试一试吗?
这是 sync.php 文件: http://pastebin.com/8iiSCXGS
非常感谢!
最佳答案
我没有深入研究您的代码,但也许这会有所帮助。
让我们从简化的数据库结构开始,如下所示:
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≤ $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 <code>block_id</code> 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,您应该会得到想要的结果。
关于PHP 自定义聊天,编码 block 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19881778/