在表中找到两条具有相同值的记录

Posted

技术标签:

【中文标题】在表中找到两条具有相同值的记录【英文标题】:Find two records in the table that have the same value 【发布时间】:2019-07-02 12:29:18 【问题描述】:

我有一张桌子b_im_message。它有列idchat_iduser_id。我无法创建查询,因此“user_id”列中有 3 或 4 个值,而 chat_id 列中有两个条目。很难描述。我会举个例子。

初始表格视图

mysql> select id, chat_id, user_id from b_im_relation;
+----+---------+---------+
| id | chat_id | user_id |
+----+---------+---------+
| 11 |       6 |       1 |
| 12 |       6 |       3 |
| 13 |       7 |       1 |
| 14 |       7 |       4 |
| 16 |       8 |       1 |
| 15 |       8 |       3 |
| 18 |       9 |       1 |
| 17 |       9 |       4 |
| 19 |      10 |       3 |
| 20 |      11 |       3 |
| 21 |      11 |       4 |
+----+---------+---------+
11 rows in set (0.00 sec)

选择值为 3 或 4 以及任何“chat_id”的行

mysql> SELECT id, chat_id, user_id 
       FROM b_im_relation 
       WHERE user_id IN (3,4) and  
             EXISTS(SELECT id, chat_id, user_id 
                    FROM b_im_relation t1 
                    WHERE EXISTS (SELECT 1 
                                  FROM b_im_relation t2 
                                  WHERE t1.chat_id=t2.chat_id and 
                                  t1.id<>t2.id));
+----+---------+---------+
| id | chat_id | user_id |
+----+---------+---------+
| 12 |       6 |       3 |
| 15 |       8 |       3 |
| 19 |      10 |       3 |
| 20 |      11 |       3 |
| 14 |       7 |       4 |
| 17 |       9 |       4 |
| 21 |      11 |       4 |
+----+---------+---------+
7 rows in set (0.00 sec)

选定对“chat_id”

mysql> SELECT id, chat_id, user_id 
       FROM b_im_relation t1 
       WHERE EXISTS (SELECT 1 FROM b_im_relation t2 
                     WHERE t1.chat_id=t2.chat_id and 
                              t1.id<>t2.id) and 
             EXISTS (SELECT id, chat_id, user_id 
                     FROM b_im_relation 
                     WHERE user_id in (3,4));
+----+---------+---------+
| id | chat_id | user_id |
+----+---------+---------+
| 11 |       6 |       1 |
| 12 |       6 |       3 |
| 13 |       7 |       1 |
| 14 |       7 |       4 |
| 16 |       8 |       1 |
| 15 |       8 |       3 |
| 18 |       9 |       1 |
| 17 |       9 |       4 |
| 20 |      11 |       3 |
| 21 |      11 |       4 |
+----+---------+---------+

我需要这个:

+----+---------+---------+
| id | chat_id | user_id |
+----+---------+---------+
| 20 |      11 |       3 |
| 21 |      11 |       4 |
+----+---------+---------+

任何值“id”,任何相同的值“cat_id”和“user_id”中的“3”或“4”

【问题讨论】:

【参考方案1】:

如果您想要chat_ids 3 和 4 所在的 chat_ids:

select i.*
from b_im_relation i inner join (
    select chat_id
    from b_im_relation
    where user_id in (3,4)
    group by chat_id
    having count(distinct user_id) = 2
) t on t.chat_id = i.chat_id

如果您想要只有user_ids 3 和 4 的 chat_ids:

select i.*
from b_im_relation i inner join (
    select chat_id
    from b_im_relation
    group by chat_id
    having count(distinct user_id) = 2 and sum(user_id not in(3, 4)) = 0
) t on t.chat_id = i.chat_id

【讨论】:

对不起,错误 1051 (42S02): 未知表 'i'【参考方案2】:

查找所有包含多条记录的用户/聊天组合,然后加入以获取所有此类记录:

select id, chat_id, user_id
from (
    select chat_id, user_id
    from b_im_relation
    where user_id in (3,4)
    group by chat_id, user_id
    having count(*) > 1
) multiple_chats
natural join b_im_relation

【讨论】:

以上是关于在表中找到两条具有相同值的记录的主要内容,如果未能解决你的问题,请参考以下文章

需要编写带有条件的查询

在表中动态记录 PLSql 语句

Netezza SQL 比较同一表中的两条记录

MySQL 索引管理与执行计划

MySQL查找具有相同customerid且具有两个不同值的记录

3.05 在一个表中查找与其他表不匹配的记录