1

我有 2 个表,Request它们Action由字段 request_id 连接。

这是一个快速的数据示例:

Request
request_id    group
100           My Group Name
101           My Group Name
102           My Group Name

Action
request_id    action_id    username
100           10           JDOE
100           11           ASMITH
101           12           SMILLER
102           13           CBROWN

我需要获取所有请求的列表以及具有多个操作的 action_id 和用户名。这是我目前拥有的,但它列出了所有具有一个或多个操作的请求。

所以结果应该显示:

request_id    action_id    username
100           10           JDOE
100           11           ASMITH

以下可以选择我需要的内容,但我不确定如何将 request_id > 1 放入其中:

SELECT request.request_id, action.action_id, action.username
FROM request RIGHT JOIN action ON request.request_id = action.request_id
WHERE (((request.group)="My Group Name"))
GROUP BY request.request_id, action.action_id, action.username;

谢谢您的帮助!!

4

2 回答 2

2

怎么样:

SELECT 
Action.request_id, 
Action.action_id, Action.username
FROM [Action] 
INNER JOIN Request ON Action.request_id = Request.request_id
WHERE Action.request_id In (
  SELECT request_id FROM action Group By Request_Id 
  HAVING Count(request_id)>1)
AND Request.group="My Group Name"
于 2013-03-28T20:00:41.517 回答
0

我认为你只需要一个having条款。这就像一个where子句,但在聚合列上:

SELECT request.request_id, action.action_id, action.username
FROM request RIGHT JOIN
     action
     ON request.request_id = action.request_id
WHERE (((request.group)="My Group Name"))
GROUP BY request.request_id, action.action_id, action.username
having counte(request.request_id) > 1
于 2013-03-28T20:01:45.500 回答