繁体   English   中英

如何使用GROUP BY子句过滤SQL查询

[英]How can I filter an SQL query with a GROUP BY clause

我正在尝试为消息传递系统制定一个SQL查询,该查询将返回状态为“OPEN”且其最后一条消息未由某个组中的用户发布的所有线程的列表。

查询中涉及的表是:

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

但此时我陷入困境,因为我无法弄清楚如何根据特定组中具有最高timestampmessages.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的成员)是最后一个发布给它们的(threadId 1中的messageId 2和threadId 4中的messageId 5)。 因此,结果集应该只包含线程2和3的线程数据(threadId,timestamp,subject,status和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

在查询中添加 LIMIT 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 LIMIT 1
 
  

或者将where子句更改为

 
 
 
  
  WHERE status='OPEN' and groupID<>1
 
  

我想这就是你想要的?

 select * from threads where threadId not in ( select messages.threadId from messages inner join (select threadId, MAX(timestamp) maxtime from messages group by threadId) t on messages.threadId = t.threadId and messages.timestamp = t.maxtime where messages.userId=1 ) 

暂无
暂无

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

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