我正在尝试为消息传递系统制定 SQL 查询,该查询将返回状态为“打开”且其最后一条消息不是由特定组中的用户发布的所有线程的列表。
查询涉及的表有:
threads
-------
threadId
timestamp
subject
status
clientId
messages
--------
messageId
userId
messagebody
timestamp
threadId
users
-----
userId
username
groupId
当前查询是:
SELECT threadId, timestamp, subject, status, clientId
FROM threads
WHERE status='OPEN'
ORDER BY timestamp DESC
效果很好;但我现在有一个新要求 - 如果线程中的最后一条消息是由 groupId
为 1 的用户发布的,那么它不应该在结果集中。
包含我需要的信息非常简单:
SELECT threadId, timestamp, subject, status, clientId
FROM threads
INNER JOIN messages USING(threadId)
INNER JOIN users USING(userId)
WHERE status='OPEN'
GROUP BY threadId
ORDER BY timestamp DESC
但在这一点上我卡住了,因为我无法弄清楚如何根据具有最高 timestamp
的 messages.userId
获取查询以过滤掉线程> 在一个特定的群体中。我查看了 HAVING
和所有聚合函数,似乎没有什么符合要求。
编辑
可能我说得不够清楚,我会试着用一个例子来说明。
这是一些示例数据:
线程
threadId | timestamp | subject | status | clientId
--------------------------------------------------
1 | 1 | One | OPEN | 1
2 | 10 | Two | OPEN | 4
3 | 26 | Three | OPEN | 19
4 | 198 | Four | OPEN | 100
消息
messageId | userId | messagebody | timestamp | threadId
-------------------------------------------------------
1 | 3 | Hello, World| 1 | 1
2 | 1 | Hello | 3 | 1
3 | 1 | ^&%&6 | 10 | 2
4 | 2 | Test | 12 | 2
5 | 4 | Hi mum | 26 | 3
6 | 1 | More tests | 100 | 4
用户
userId | username | groupId
---------------------------
1 | Gareth | 1
2 | JimBob | 2
3 | BillyBob | 2
4 | Bod | 3
在这个例子中数据集线程 1 和 4 应该从查询中删除,因为用户 Gareth
(组 1 的成员)是最后一个发布给他们的(messageId 2 in threadId 1,并且threadId 4 中的 messageId 5)。因此结果集应该只有线程 2 和线程 3 的线程数据(threadId、时间戳、主题、状态和 clientId)。
我创建了一个 SQLfiddle对于这个测试数据。
谁能给我指出正确的方向?
最佳答案
听起来你想要这个:
SELECT threadId, timestamp, subject, status, clientId
FROM threads t
INNER JOIN messages m
ON t.threadId = m.threadId
INNER JOIN users u
ON m.userId = u.userId
and u.groupId != 1 --placed the groupId filter here.
WHERE status='OPEN'
GROUP BY threadId
ORDER BY timestamp DESC
编辑,这似乎可以满足您的需求:
SELECT t.threadId,
t.timestamp,
t.subject,
t.status,
t.clientId
FROM threads t
INNER JOIN messages m
ON t.threadId = m.threadId
INNER JOIN users u
ON m.userId = u.userId
WHERE status='OPEN'
AND NOT EXISTS (select t1.threadid
FROM threads t1
INNER JOIN messages m
ON t1.threadId = m.threadId
INNER JOIN users u
ON m.userId = u.userId
where u.groupid = 1
and t.threadid = t1.threadid)
ORDER BY timestamp DESC
参见SQL Fiddle with Demo
关于mysql - 如何使用 GROUP BY 子句过滤 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424127/