简体   繁体   English

MYSQL选择/分组依据:如何根据其中一个成员的值从结果中删除一个分组?

[英]MYSQL Select/Group By: How do I remove a group from the result based on a value of one of it's members?

Consider the table of events below. 请考虑下面的事件表。 The OrderID field is a foreign key to an Orders table. OrderID字段是Orders表的外键。

OrderID     EventType     TimeAdded*       Processed
   1           ...        3 Hours Ago          0
   2           ...        5 Minutes Ago        0
   1           ...        2 Hours Ago          0
   1           ...        1 Hour Ago           0    
   3           ...        12 Minutes ago       1
   3           ...        3 Hours Ago          0
   2           ...        2 Hours Ago          0

*TimeAdded is actual a mysql date-time field. * TimeAdded是实际的mysql日期时间字段。 I used human readable times here to make the question easier to read. 我在这里使用人类可读的时间来使问题更易于阅读。


I need to get a result set that has each OrderID, only once, but only if ALL records attached to a given order ID, that have a proceeded status of 0, have been added more than 15 minutes ago. 我需要获得一个仅具有一次每个OrderID的结果集,但前提是在15分钟前添加了附加到给定订单ID的所有记录,这些记录的前进状态为0。

In this example, order #2 should be OMITTED from the result set because it has an unprocessed event added within the last 15 minutes. 在此示例中,应该从结果集中忽略订单#2,因为它在最近15分钟内添加了未处理的事件。 The fact that it has an unprocessed event from 2 hours ago is inconsequential. 从2小时前开始有未处理的事件这一事实是无关紧要的。

Order #3 SHOULD be included. 订单#3应该包括在内。 Even though one of its entries was added 12 minutes ago, that entry has already been processed, and should not be considered. 即使其中一个条目是在12分钟前添加的,该条目也已被处理,因此不应考虑。 Only the "3 hours ago" entry should be considered in this set, and it is greater than 15 minutes ago. 此集合中仅应考虑“ 3小时前”条目,并且该条目大于15分钟前。

Order #1 SHOULD be included, since all three unprocessed events attached to it occurred more than 15 minutes ago. 应该包含订单#1,因为与其相关的所有三个未处理事件都在15分钟前发生。

I'm not sure if this needs to use a sub-query or group by or possibly both? 我不确定这是否需要使用子查询或分组依据,或者可能同时使用?

pseudo code: 伪代码:

SELECT * FROM OrderEvents WHERE Processed=0 GROUP BY OrderID
    OMIT WHOLE GROUP IF GROUP_MIN(TimeAdded) >= (NOW() - INTERVAL 15 MINUTE)

I'm just not certain of the correct syntax? 我只是不确定正确的语法?

This will do the work (tested in MySQL Workbench): 这将完成工作(在MySQL Workbench中测试):

SELECT * FROM OrderEvents WHERE Processed = 0 GROUP BY OrderID
    HAVING MAX(TimeAdded) < (NOW() - INTERVAL 15 MINUTE);

I think it's something along these lines: 我认为这是遵循以下原则的:

SELECT OrderID
FROM OrderEvents
WHERE Processed=0
GROUP BY OrderID
HAVING MAX(TimeAdded) < (NOW() - 1500);

Bear in mind, that I'm working blind here. 请记住,我在这里盲目工作。 I think 1500 is correct, for the function now() used in a numeric context. 我认为1500是正确的,对于在数字上下文中使用的now()函数。 http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now

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

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