1

我试图构建一个 Block 函数,但没有任何运气,我的 SQL 技能不如我希望的那样好。

我有一个名为“消息”的表和一个名为“块”的表现在,有 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

在“blocks”中,user_id 是块行的所有者 ID。而block_id是所有者想要屏蔽的id。IF "messages.source_id = blocks.block_id OR messages.block_id = blocks.user_id" THAN 不要让消息通过。我知道要求某人为我编写代码是很粗鲁的,但我在问,有人可以试一试吗?

这是 sync.php 文件: http ://pastebin.com/8iiSCXGS

非常感谢!

4

1 回答 1

1

我没有深入研究您的代码,但也许这会有所帮助。

让我们从简化的数据库结构开始,如下所示:

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)。要确定可以显示哪些消息,我们需要执行messagesblocks表的左连接 - 即,我们要考虑包含相关信息的每一行以及包含相关信息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 替换到此查询中,您应该会得到您想要的结果。

于 2013-11-10T00:26:59.380 回答