从一个表中获取不在另一个表中的所有数据

3zwjbxry  于 2021-06-19  发布在  Mysql
关注(0)|答案(3)|浏览(321)

我有一个消息表:

mysql> describe messages;
+-----------+--------------+------+-----+-------------------+-----------------------------+
| Field     | Type         | Null | Key | Default           | Extra                       |
+-----------+--------------+------+-----+-------------------+-----------------------------+
| id        | int(11)      | NO   | PRI | NULL              | auto_increment              |
| user1_id  | int(11)      | NO   |     | NULL              |                             |
| user1     | varchar(255) | NO   |     | NULL              |                             |
| user2_id  | int(11)      | NO   |     | NULL              |                             |
| user2     | varchar(255) | NO   |     | NULL              |                             |
| message   | text         | YES  |     | NULL              |                             |
| timestamp | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user1read | varchar(3)   | NO   |     | NULL              |                             |
| user2read | varchar(3)   | NO   |     | NULL              |                             |
+-----------+--------------+------+-----+-------------------+-----------------------------+
9 rows in set (0.00 sec)

然后我有一个被阻塞的表:

mysql> describe blocked;
+-----------------+---------+------+-----+---------+-------+
| Field           | Type    | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+-------+
| user_id         | int(11) | NO   | PRI | NULL    |       |
| blocked_user_id | int(11) | NO   | PRI | NULL    |       |
+-----------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

我要做的是让所有的数据显示在一个页面上的不同div中。此脚本每秒调用一次。我最终得到的查询产生了所有消息,无论阻塞状态如何。
所以,假设我的id是1,一个被阻止的用户的id是5,那么用户5不应该出现在列表中。如果其他用户登录,也会出现同样的情况。
在搞乱这个查询一段时间后,我尝试了第二个查询。更糟糕的是,尽管使用同一查询以手动sql条目返回数据,但页面上没有显示任何内容。
这个文件很小,所以我就把整个东西贴出来:
编辑:第一个查询实际上显示了所有消息,包括作为消息的我自己,这显然是错误的。看来我的问题比我想象的更糟。

<?php
session_start();

include '../../../config/DB.php';

$username = $_SESSION['logged_in']; //to use in queries

try {
    $db = new DB(); //new DB object
} catch (Exception $e) {
    $e->getMessage();
}

try {
    $names = array(); //to store during foreach iterations

    //get the id for the other query attempt
    $id_result = $db->getRow('SELECT id FROM users WHERE username=?', [$username]);
    $id = $id_result['id'];

    foreach ($messages_result = $db->getRows('SELECT messages.user1, messages.user2, messages.timestamp, messages.message, messages.user2read, users.avatar
                                                    FROM messages
                                                    LEFT JOIN users ON messages.user1 = users.username
                                                    WHERE messages.user2 = ? AND 
                                                    (users.id NOT IN (SELECT user_id FROM blocked))
                                                    OR (users.id NOT IN (SELECT blocked_user_id FROM blocked))
                                                    ORDER BY timestamp DESC', [$username]) as $result) {
        $sender = $result['user1'];
        $time = $result['timestamp'];
        $message = $result['message'];
        $avatar = $result['avatar'];
        $user2read = $result['user2read'];

        //Do this so users will only show once.
        //One div per user, and when clicked
        //all messages are shown elsewhere on a page.
        if (!in_array($sender, $names)) {
            $names[] = $sender;

            //If the message is unread, show name in bold
            //Else show regular text
            //$avatar has been removed from the html for now
            //It shows up in an <img> tag
            if ($user2read === 'no') {
                echo '<div id="single_message" data-sender="' . $sender . '"><p style="padding-left:8px;"><p><a class="link" style="font-weight:bold;font-size:16px;" href=' . $sender . '>' . $sender . '</a></strong></p><p style="white-space:pre-wrap;margin-left:8px;margin-right:8px;">' . $message . '</p><p style="padding-left:8px;border-bottom: 1px solid #ccc;">' . $time . '</p></div>';
            } else {
                echo '<div id="single_message" data-sender="' . $sender . '"><p style="padding-left:8px;"><p><a class="link" href=' . $sender . '>' . $sender . '</a></p><p style="white-space:pre-wrap;margin-left:8px;margin-right:8px;">' . $message . '</p><p style="padding-left:8px;border-bottom: 1px solid #ccc;">' . $time . '</p></div>';
            }

        } else {
            continue;
        }
    }

} catch (Exception $e) {
}

这是我尝试的第二个查询(我将显示foreach)。这是一个没有显示在页面上。

foreach($messages_result = $db->getRows('SELECT * FROM messages m LEFT JOIN blocked b ON ((m.user1_id = b.user_id OR m.user2_id = b.user_id)
                                                    AND (m.user1_id = b.blocked_user_id OR m.user2_id = b.blocked_user_id))
                                                    WHERE (m.user1_id = 1 OR m.user2_id = 1) HAVING m.user_id IS NULL
                                                    ORDER BY timestamp DESC', [$id]) as $result) {

我是不是漏了什么?

83qze16e

83qze16e1#

WHERE (m.user1_id = 1 OR m.user2_id = 1) HAVING m.user_id IS NULL 我认为这是相互矛盾的,因为你有这个加入 ON ((m.user1_id = b.user_id OR m.user2_id = b.user_id) AND (m.user1_id = b.blocked_user_id OR m.user2_id = b.blocked_user_id)) ……就像互相抵消。。。。。。。。
纠正这个…也许可以…祝你好运

mrfwxfqh

mrfwxfqh2#

我有一个类似的脚本,它从users表中获取所有未被阻止的用户,而不是从messages表中获取数据。因为这只比较了用户id和被阻止的id,所以我可以用一个查询块来完成。但是,我需要检查消息中的user1\u id和user2\u id,以及blocked中的user\u id和blocked\u user\u id之间的所有可能性。这需要一个额外的块,它是第一个块的反向版本。
我将通过显示用户的id和用户名,然后显示阻塞表中的所有人,然后显示使用阻塞表中的id的结果来说明这是如何成功的。

mysql> select id, username from users;
+----+-----------------+
| id | username        |
+----+-----------------+
|  1 | csheridan       |
|  2 | testuser        |
|  3 | testuser2       |
|  4 | washington_user |
+----+-----------------+
4 rows in set (0.00 sec)

mysql> select * from blocked;
+---------+-----------------+
| user_id | blocked_user_id |
+---------+-----------------+
|       1 |               2 |
|       1 |               4 |
+---------+-----------------+
2 rows in set (0.00 sec)

mysql> SELECT m.* FROM messages m   
       WHERE NOT EXISTS (SELECT 1 FROM blocked b 
       WHERE b.user_id = m.user1_id AND b.blocked_user_id = 1) 
       AND NOT EXISTS 
       (SELECT 1 FROM blocked b WHERE b.blocked_user_id = m.user1_id AND b.user_id = 1)   
       AND NOT EXISTS (SELECT 1 FROM blocked b WHERE b.user_id = m.user2_id AND b.blocked_user_id = 1) 
       AND NOT EXISTS (SELECT 1 FROM blocked b WHERE b.blocked_user_id = m.user2_id AND b.user_id = 1) 
       ORDER BY timestamp DESC;
+----+----------+-----------+----------+-----------+----------------+---------------------+-----------+-----------+
| id | user1_id | user1     | user2_id | user2     | message        | timestamp           | user1read | user2read |
+----+----------+-----------+----------+-----------+----------------+---------------------+-----------+-----------+
|  2 |        3 | testuser2 |        1 | csheridan | Hey!           | 2018-11-14 12:12:35 | yes       | no        |
|  4 |        3 | testuser2 |        1 | csheridan | Are you there? | 2018-11-14 12:12:35 | yes       | no        |
+----+----------+-----------+----------+-----------+----------------+---------------------+-----------+-----------+
2 rows in set (0.00 sec)

mysql> SELECT m.* FROM messages m   
       WHERE NOT EXISTS (SELECT 1 FROM blocked b  
       WHERE b.user_id = m.user1_id AND b.blocked_user_id = 2) 
       AND NOT EXISTS (SELECT 1 FROM blocked b WHERE b.blocked_user_id = m.user1_id AND b.user_id = 2)   
       AND NOT EXISTS (SELECT 1 FROM blocked b WHERE b.user_id = m.user2_id AND b.blocked_user_id = 2) 
       AND NOT EXISTS (SELECT 1 FROM blocked b WHERE b.blocked_user_id = m.user2_id AND b.user_id = 2) 
       ORDER BY timestamp DESC;
+----+----------+-----------------+----------+----------+---------------------+---------------------+-----------+-----------+
| id | user1_id | user1           | user2_id | user2    | message             | timestamp           | user1read | user2read |
+----+----------+-----------------+----------+----------+---------------------+---------------------+-----------+-----------+
|  7 |        4 | washington_user |        2 | testuser | Hey man! What's up? | 2018-11-14 14:32:27 | yes       | no        |
|  8 |        4 | washington_user |        2 | testuser | Hello there.        | 2018-11-14 14:32:27 | yes       | no        |
+----+----------+-----------------+----------+----------+---------------------+---------------------+-----------+-----------+
2 rows in set (0.00 sec)

所以,问题是我在正确的轨道上,我只需要另一个街区。首先,我检查了blocked.user\u id=messages.user1\u id和blocked.user\u id=?,然后检查了user2\u id的其他条件。除非有人提出更紧凑的查询,否则这就是我所需要的。如果我只做了第一个块,它没有检查所有条件,仍然返回被阻止的用户,因为它没有检查消息中的user1\u id和user2\u id。

l3zydbqr

l3zydbqr3#

匹配为的被阻止用户的左连接 user2 (“我”)到 blocked.user_id ,阻挡的人,以及 user1 反对 blocked.blocked_user_id 被阻止的用户,如果结果为null(aka b.user\u id为null),则不存在阻止。

foreach($messages_result = $db->getRows('
SELECT messages.user1, messages.user2, messages.timestamp, messages.message, messages.user2read, users.avatar
FROM messages
LEFT JOIN users ON messages.user1 = users.username
LEFT JOIN blocked b ON b.user_id = messages.user2_id AND b.blocked_user_id = messages.user1_id
WHERE messages.user2 = ? AND b.user_id IS NULL
ORDER BY timestamp DESC',[$username]) as $result) {
...

相关问题