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

pcww981p  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(266)

我有一张table order_history 其中包含了我按顺序执行的所有操作的记录,如 confirm order , attempted order , reject order 或者 revert order .
当员工将订单标记为 attempt 它创建了一个历史,经过一段时间 revert 那个秩序回到了它原来的地方(它也创造了历史)。在那之后,另一个雇员标记为相同的顺序 attempt .
问题
现在, history 表中有两条顺序相同的记录。我只想选最新的 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 =员工和行动id 2 for attempt 以及 3 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 但订单555应该只为用户7显示。
预期产量

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

注:555号订单上的所有操作都是在同一天执行的
让我知道是否需要更多的细节。

b5lpy0ml

b5lpy0ml1#

预期输出与尝试的代码不符。如果你只想要最新的尝试,那么你需要看看尝试和逆转。

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根据历史\ 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)

相关问题