我還沒有深入研究過你的代碼,但也許這會有所幫助。
讓我們先從一個簡化數據庫結構如下:
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)
正如你所看到的,我們想要的行是其中最後三米欄是空的那些,這意味着沒有影響向特定用戶顯示此特定消息的阻止規則。因此,要提取這些信息,我們只需添加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添加到這個查詢,你應該得到你之後的結果。
所以messages.source_id是消息創建者的ID嗎?這是一個私人消息傳遞操作嗎?從創建者的角度來看,如果他們阻止了某人,那麼您希望以創建該消息的形式限制它們的可用性。從被阻止的用戶的角度來看,你想讓他們知道他們是否被阻止? – sark9012
你好,是的,這是正確的!我希望sync.php不要拍攝來自阻止我並且被阻止的用戶的消息。如上所示,所有塊都存儲在塊表中。在私人聊天中我修復了Block功能!但是在開放的房間裏,每個人都可以互相講話,sync.php文件會在用戶之間發送所有消息,這就是爲什麼sync.php必須檢查blocks表以查看它是否應該接收消息,這是非常複雜的(至少我...) – user2965339
好吧,它仍然會將消息存儲在數據庫中,但不會顯示它們。我相信這可以通過將表格連接在一起來實現。 – sark9012