从数据库中选择最新的历史记录

时间:2018-07-07 06:43:43

标签: mysql sql database mysqli php-mysqlidb

专家,我陷入了混乱。我有一个表order_history,其中包含我按顺序执行的所有操作的记录,例如confirm orderattempted orderreject orderrevert order

当某个员工将订单标记为attempt时,它将创建一个历史记录,并在一段时间后管理revert将该订单恢复到其原始位置(它还会创建历史记录)。之后,其他一些员工标记了与attempt相同的订单。

问题

现在,history表具有2个相同顺序的尝试记录。而且我只想选择最新的attempted历史记录,因为先前的操作已还原。

数据库结构

|history_id |order_id | date_added   |user_id | action_id|
|-----------|---------|--------------|--------|----------|
| 13        | 444     | 2018/07/06   |   9    |2         |
| 12        | 555     | 2018/07/05   |   7    |2         |
| 11        | 444     | 2018/07/05   |   2    |3         |
| 10        | 555     | 2018/07/05   |   2    |3         |
| 9         | 555     | 2018/07/05   |   4    |2         |
| 8         | 444     | 2018/07/04   |   1    |2         |

user_id = Employee和action_id 2 for attempt3 for revert back的地方,当尝试下订单然后还原然后再由其他员工尝试时,我的查询在员工A和B中都重复了该订单但应该显示在最新的员工帐户中。

我的查询

SELECT COUNT(oh.order_id) AS total_attempted,
       oh.user_id
FROM `order_history` oh 
WHERE oh.action_id = '2'
GROUP BY oh.user_id

结果

此查询向两个用户order ID : 555显示user_id: 4 and 7,但只为用户7显示订单555。

预期产量

|history_id |order_id | date_added   |user_id | action_id|
|-----------|---------|--------------|--------|----------|
| 13        | 444     | 2018/07/06   |   9    |2         |
| 12        | 555     | 2018/07/05   |   7    |2         |

PS:对555号订单的所有操作均在同一日期执行

让我知道是否需要更多详细信息。

1 个答案:

答案 0 :(得分:1)

您的预期输出与您尝试的代码不一致。如果只需要最新尝试,则需要查看尝试和还原。

drop table if exists oh;
create table oh (history_id int,order_id int,date_added varchar(100),user_id int,action_id int);
insert into oh (history_id ,order_id,date_added,user_id,action_id) values(13,444,"2018/07/06",9,2);
insert into oh (history_id ,order_id,date_added,user_id,action_id) values(12,555,"2018/07/05",7,2);
insert into oh (history_id ,order_id,date_added,user_id,action_id) values(11,444,"2018/07/05",2,3);
insert into oh (history_id ,order_id,date_added,user_id,action_id) values(10,555,"2018/07/05",2,3);
insert into oh (history_id ,order_id,date_added,user_id,action_id) values(9,555,"2018/07/05",4,2);
insert into oh (history_id ,order_id,date_added,user_id,action_id) values(8,444,"2018/07/04",1,2);
insert into oh values(7,333,"2018/07/04",1,3),(6,333,"2018/07/04",1,2),
(5,222,"2018/07/04",1,2),(4,222,"2018/07/04",2,2),
(3,111,"2018/07/04",1,2);

子查询s根据history_id查找最新操作(我假设这表明了事件的顺序)

此代码列出了最新的尝试

select * from
(
select *
from   oh 
where  action_id in (2,3) and 
         history_id = (select max(history_id) from oh oh1 where oh1.order_id = oh.order_id)
) s
where s.action_id = 2;

+------------+----------+------------+---------+-----------+
| history_id | order_id | date_added | user_id | action_id |
+------------+----------+------------+---------+-----------+
|         13 |      444 | 2018/07/06 |       9 |         2 |
|         12 |      555 | 2018/07/05 |       7 |         2 |
|          5 |      222 | 2018/07/04 |       1 |         2 |
|          3 |      111 | 2018/07/04 |       1 |         2 |
+------------+----------+------------+---------+-----------+
4 rows in set (0.02 sec)

此代码计算尝试次数(不包括用户返回的次数)

select user_id,count(*) attempts
from
(
select *
from   oh 
where  action_id in (2,3) and 
         history_id = (select max(history_id) from oh oh1 where oh1.order_id = oh.order_id)
) s
where s.action_id = 2
group by user_id;

+---------+----------+
| user_id | attempts |
+---------+----------+
|       1 |        2 |
|       7 |        1 |
|       9 |        1 |
+---------+----------+
3 rows in set (0.00 sec)