MySQL多行,其值在2或行中具有特定值,并且具有另一个状态
我有一个带有项目ID,状态和创建的表格和关联order_id的表'item_status_history'。该表拥有项目经历的状态历史。
我正在尝试查询所有具有多个状态=“已发货”的item_id。 “返回”后必须至少有一个运输状态。
在图片中,我们可以看到第一组,第三组是我需要的所需item_id。 第四组数据已发货2张,但它们是“返回”之前的。 请注意,状态不是由Create_at订购的。我需要检查返回状态后是否有发货状态。
我尝试了一些事情。我试图选择所有具有状态的ITEM_ID及其ID,并在子查询中使用它,以再次找到已发货的状态,但不等于相同的ID。这不起作用,
select item_id from item_status_history as ISH where ISH.status = "Shipped" and ISH.item_id in (Select
item_id
from item_status_history
Where status = "Shipped" and item_status_history.id <> ISH.id
)
我知道这只是其中的一部分,然后我需要从生成的ID中找到哪些ID以及在发货之后返回的。
I have a table 'Item_status_history' with Item id, Status and created_at and associated order_id. This table holds an history of status an item has gone through.
I am trying to query all item_id which have multiple status = "Shipped".
and there must be at least one shipped status after a "Returned".
In the picture we can see the first set and the third set are the desired item_id I need.
The fourth set of data has 2 shipped but they were before "returned".
Note that the statuses are not ordered by created_at. I need to check if there is a shipped status after the return status was sent.
I have tried a few things. I tried to select all the item_id and its id with status shipped and use this in a subquery to again find status with shipped but not equal to the same id. THis is not working
select item_id from item_status_history as ISH where ISH.status = "Shipped" and ISH.item_id in (Select
item_id
from item_status_history
Where status = "Shipped" and item_status_history.id <> ISH.id
)
This i know is only a part of it, then I need to find from the resulting ids, which ones have returned and if it is after the shipped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它一定是一排独特的行,也许是一些ID?
但是,我们可以假设,而不是
发货
和retored
与返回
之后相比,是同一时间实现。It must be some unique order of rows , maybe some ID ?
But we can assume, than if
Shipped
andRetured
is in the same time, than condition aboutShipped
afterReturned
is fulfilled .