mysql 从 table 中检索和操作当前行和上一行
mysql Retrieve and manipulate current row and previous row from table
我看了好几个地方,也许我的搜索措辞不正确。我发现了类似的问题,但其中 none 个回答了问题。
我有一个 table 现货库存(用户走过存储区域并实际检查手头有多少产品)。 table 处理多个位置。 table结构(部分,仅需要的信息)为:
create table location_inventory (
id int unsigned not null auto_increment,
location_id int unsigned references location(location_id),
inventory_item_id int unsigned references inventory_item (inventory_item_id),
inventory_date date comment 'Date the sight inventory was taken',
quantity decimal( 15,2 ) comment 'Number of items found during inventory',
primary key ( id ),
unique (location_id,inventory_item_id,inventory_date)
);
应该是以下形式的查询:
select
a.location_id location,
a.inventory_item_id inventory_item,
a.inventory_date curr_date,
a.quantity curr_quant,
b.inventory_date prev_date,
b.quantity prev_quant,
a.quantity - b.quantity num_used
from
location_inventory a
left outer join
(
select
location_id,
inventory_item_id,
inventory_date,
quantity
from
location_inventory
where
something
) b
on ( location_id,inventory_item_id )
where
a.inventory_date between DATEA and DATEB
但我还没有让它工作。
我缺少的是整个子查询。我已经看到了几个我们得到上一个日期的答案,但是 none 我实际上可以从上一行中检索其余的值;它最终检索整个 table.
中最新条目的值
当您选择仅显示 table 结构的一部分时,您可以省略我们可能需要的内容。下面我假设您有一列 id
作为每一行的唯一标识符
SELECT
a.location_id location
, a.inventory_item_id inventory_item
, a.inventory_date curr_date
, a.quantity curr_quant
, b.inventory_date prev_date
, b.quantity prev_quant
, a.quantity - b.quantity num_used
FROM (
SELECT
*
, (SELECT id
FROM location_inventory
WHERE location_id = t.location_id
and inventory_item_id = t.inventory_item_id
and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
) prev_id
FROM location_inventory t
) a
LEFT OUTER JOIN location_inventory b ON a.prev_id = b.id
WHERE a.inventory_date BETWEEN DATEA AND DATEB
另一种方法是对每个需要的值使用相关子查询:
SELECT
a.location_id location
, a.inventory_item_id inventory_item
, a.inventory_date curr_date
, a.quantity curr_quant
, (SELECT inventory_date
FROM location_inventory
WHERE location_id = t.location_id and inventory_item_id = t.inventory_item_id and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
) prev_date
, (SELECT quantity
FROM location_inventory
WHERE location_id = t.location_id and inventory_item_id = t.inventory_item_id and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
) prev_quant
, a.quantity
- (SELECT quantity
FROM location_inventory
WHERE location_id = t.location_id and inventory_item_id = t.inventory_item_id and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
)
FROM location_inventory t
WHERE a.inventory_date BETWEEN DATEA AND DATEB
自版本 8.0 发布以来 MySQL 支持 window 功能,例如 lag()
,这使得这变得更加容易和高效。
SELECT
a.location_id location
, a.inventory_item_id inventory_item
, a.inventory_date curr_date
, a.quantity curr_quant
, lag(inventory_date,1) over(partiton by location_id,inventory_item_id order by inventory_date DESC) prev_date
, lag(quantity,1) over(partiton by location_id,inventory_item_id order by inventory_date DESC) prev_quant
, a.quantity
- lag(quantity,1) over(partiton by location_id,inventory_item_id order by inventory_date DESC) num_used
FROM location_inventory a
WHERE a.inventory_date BETWEEN DATEA AND DATEB
最后说明:我不赞成依赖于查询序列的别名方案
我看了好几个地方,也许我的搜索措辞不正确。我发现了类似的问题,但其中 none 个回答了问题。
我有一个 table 现货库存(用户走过存储区域并实际检查手头有多少产品)。 table 处理多个位置。 table结构(部分,仅需要的信息)为:
create table location_inventory (
id int unsigned not null auto_increment,
location_id int unsigned references location(location_id),
inventory_item_id int unsigned references inventory_item (inventory_item_id),
inventory_date date comment 'Date the sight inventory was taken',
quantity decimal( 15,2 ) comment 'Number of items found during inventory',
primary key ( id ),
unique (location_id,inventory_item_id,inventory_date)
);
应该是以下形式的查询:
select
a.location_id location,
a.inventory_item_id inventory_item,
a.inventory_date curr_date,
a.quantity curr_quant,
b.inventory_date prev_date,
b.quantity prev_quant,
a.quantity - b.quantity num_used
from
location_inventory a
left outer join
(
select
location_id,
inventory_item_id,
inventory_date,
quantity
from
location_inventory
where
something
) b
on ( location_id,inventory_item_id )
where
a.inventory_date between DATEA and DATEB
但我还没有让它工作。
我缺少的是整个子查询。我已经看到了几个我们得到上一个日期的答案,但是 none 我实际上可以从上一行中检索其余的值;它最终检索整个 table.
中最新条目的值当您选择仅显示 table 结构的一部分时,您可以省略我们可能需要的内容。下面我假设您有一列 id
作为每一行的唯一标识符
SELECT
a.location_id location
, a.inventory_item_id inventory_item
, a.inventory_date curr_date
, a.quantity curr_quant
, b.inventory_date prev_date
, b.quantity prev_quant
, a.quantity - b.quantity num_used
FROM (
SELECT
*
, (SELECT id
FROM location_inventory
WHERE location_id = t.location_id
and inventory_item_id = t.inventory_item_id
and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
) prev_id
FROM location_inventory t
) a
LEFT OUTER JOIN location_inventory b ON a.prev_id = b.id
WHERE a.inventory_date BETWEEN DATEA AND DATEB
另一种方法是对每个需要的值使用相关子查询:
SELECT
a.location_id location
, a.inventory_item_id inventory_item
, a.inventory_date curr_date
, a.quantity curr_quant
, (SELECT inventory_date
FROM location_inventory
WHERE location_id = t.location_id and inventory_item_id = t.inventory_item_id and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
) prev_date
, (SELECT quantity
FROM location_inventory
WHERE location_id = t.location_id and inventory_item_id = t.inventory_item_id and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
) prev_quant
, a.quantity
- (SELECT quantity
FROM location_inventory
WHERE location_id = t.location_id and inventory_item_id = t.inventory_item_id and inventory_date < t.inventory_date
ORDER BY inventory_date DESC
LIMIT 1
)
FROM location_inventory t
WHERE a.inventory_date BETWEEN DATEA AND DATEB
自版本 8.0 发布以来 MySQL 支持 window 功能,例如 lag()
,这使得这变得更加容易和高效。
SELECT
a.location_id location
, a.inventory_item_id inventory_item
, a.inventory_date curr_date
, a.quantity curr_quant
, lag(inventory_date,1) over(partiton by location_id,inventory_item_id order by inventory_date DESC) prev_date
, lag(quantity,1) over(partiton by location_id,inventory_item_id order by inventory_date DESC) prev_quant
, a.quantity
- lag(quantity,1) over(partiton by location_id,inventory_item_id order by inventory_date DESC) num_used
FROM location_inventory a
WHERE a.inventory_date BETWEEN DATEA AND DATEB
最后说明:我不赞成依赖于查询序列的别名方案