简体   繁体   English

MySQL查询选择具有相同值的所有字段

[英]MySQL query select all fields with same value

be I'm looking for help in writing a MySQL query. 我在寻找MySQL查询的帮助。

Each row has a minimum of two rows with the same conversation_id. 每行至少有两行,具有相同的conversation_id。 I want to select those that that have "delete" in the recipient_state field in all rows with the same conversation_id. 我想在具有相同conversation_id的所有行的recipient_state字段中选择那些具有“删除”的那些。

SELECT conversation_id
FROM xf_conversation_recipient
GROUP BY recipient_state HAVING (delete in all fields)

The following conversation_id would be selected 将选择以下conversation_id

conversation_id recipient_state
1               delete
1               delete

The following conversation_id would NOT be selected 不会选择以下conversation_id

conversation_id recipient_state
1               delete
1               active
SELECT conversation_id, COUNT(DISTINCT recipient_state) AS nb, recipient_state
FROM xf_conversation_recipient
GROUP BY conversation_id
HAVING nb=1 AND recipient_state='delete'

The query groups by conversation_id, keeps only records having 1 distinct recipient_state and recipient_state is equal to 'delete'. 通过conversation_id的查询分组仅保留具有1个不同的recipient_state且recipient_state等于“delete”的记录。

Best I can come up with is a self-left-join: 我能想出的最好的是自我左联:

SELECT DISTINCT r1.conversation_id
FROM xf_conversation_recipient AS r1
LEFT JOIN xf_conversation_recipient AS r2 ON r2.conversation_id = r1.conversation_id AND r2.recipient_state != 'delete'
WHERE r1.recipient_state = 'delete' AND r2.conversation_id IS NULL

Basically, grab all rows where there are no matching rows that don't have "delete" as the state. 基本上,抓住没有匹配行的所有行,这些行没有“删除”作为状态。

Given a table xf_conversation_recipient that looks like this: 给定一个表xf_conversation_recipient ,如下所示:

+----------------+----------------+
|conversation_id |recipient_state |
+----------------+----------------+
|1               |delete          |
|1               |delete          |
|2               |active          |
|2               |delete          |
|3               |delete          |
|3               |delete          |
|4               |active          |
|4               |delete          |
|5               |active          |
|5               |active          |
|6               |delete          |
|6               |delete          |
+----------------+----------------+

The following query returns the IDs of all conversations matching your criteria 以下查询返回符合条件的所有会话的ID

SELECT 
    conversation_id AS selectedId, 
    count(*) AS count    
FROM xf_conversation_recipient   
WHERE recipient_state = "delete" 
GROUP BY conversation_id 
HAVING count>1 

Returns: 返回:

+-----------+------+
|selectedId |count |
+-----------+------+
|1          |2     |
|3          |2     |
|6          |2     |
+-----------+------+

Note, depending on your application, you may want to stop here. 请注意,根据您的应用程序,您可能希望在此处停止。


Nesting this query, we can extract only the selectedId column, and nesting it again we can use that as an IN condition on a query, like so: 嵌套此查询,我们只能提取selectedId列,并再次嵌套,我们可以将其用作查询的IN条件,如下所示:

SELECT * FROM xf_conversation_recipient 
WHERE xf_conversation_recipient.conversation_id IN (
    SELECT t1.selectedId FROM (
        SELECT 
            conversation_id AS selectedId, 
            count(*) AS count
        FROM xf_conversation_recipient
        WHERE recipient_state = "delete"
        GROUP BY conversation_id
        HAVING count>1
    ) t1  
)

Which returns: 哪个回报:

+----------------+----------------+
|conversation_id |recipient_state |
+----------------+----------------+
|1               |delete          |
|1               |delete          |
|3               |delete          |
|3               |delete          |
|6               |delete          |
|6               |delete          |
+----------------+----------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM