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 |
+----------------+----------------+
次のクエリは、条件に一致するすべての会話の ID を返します
SELECT
conversation_id AS selectedId,
count(*) AS count
FROM xf_conversation_recipient
WHERE recipient_state = "delete"
GROUP BY conversation_id
HAVING count>1
戻り値:
+-----------+------+
|selectedId |count |
+-----------+------+
|1 |2 |
|3 |2 |
|6 |2 |
+-----------+------+
アプリケーションによっては、ここで終了する場合があることに注意してください。
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
)
どちらが返されますか:
+----------------+----------------+
|conversation_id |recipient_state |
+----------------+----------------+
|1 |delete |
|1 |delete |
|3 |delete |
|3 |delete |
|6 |delete |
|6 |delete |
+----------------+----------------+