Mysql 如何仅在列自上次输入后发生更改时插入? 问题描述
Mysql 如何仅在列自上次输入后发生更改时插入? 问题描述,mysql,sql,Mysql,Sql,RDMS:10.1.23-MariaDB create table product_log ( id int auto_increment, id_product mediumint(6) unsigned not null, id_store int not null, quantity int default 0 not null, date_log date null, constraint product_log_pk pr
RDMS:10.1.23-MariaDB
create table product_log
(
id int auto_increment,
id_product mediumint(6) unsigned not null,
id_store int not null,
quantity int default 0 not null,
date_log date null,
constraint product_log_pk
primary key (id)
);
create table product_store
(
id int auto_increment,
id_product mediumint(6) unsigned not null,
id_store int not null,
quantity int default 0 not null,
constraint product_store_pk
primary key (id)
);
INSERT INTO product_store SET id_product = 1, id_store = 1, quantity = 6;
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps WHERE ps.id_store = 1
);
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps WHERE ps.id_store = 1
); -- I wish this does not add a line because quantity has not changed
UPDATE product_store SET quantity = 12 WHERE id_product = 1 AND id_store = 1;
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps WHERE ps.id_store = 1
); -- I wish this does add a line because quantity has changed
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps WHERE ps.id_store = 1
); -- I wish this does not add a line because quantity has not changed
SQL Fiddle:
我有一个由以下字段组成的表
product\u log
:
id (int not null primary key auto increment)`
id_product (int not null foreign key)
id_store (int not null foreign key)
quantity(int unsigned not null)
date_log(date)
我在cron作业中有一个查询,它在PHP中循环我的存储,并对我的所有存储执行此查询:
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps WHERE ps.id_store = :store
)
:将
存储为通配符(我使用PHP的PDO进行查询)
我的查询目前运行良好,但我只想在数量自上次记录以来发生变化的情况下插入该行。
尝试过的解决方案
我尝试在重复键更新上使用唯一约束和子句,但无效,因为我无法检查重复的数量是否是最后一个日志(如果数量已变为2->1->2,我希望记录条目,因为2 1,但唯一约束将介入,我不希望这样。)
我想可能会有一些自我连接的恶作剧,但我想我没有足够的技能去发现它。如果有人能提供一个能解决我问题的查询,我将不胜感激
我也想到了这个问题:
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps
WHERE ps.id_store = :store AND ps.quantity <> (
SELECT IF(COUNT(pl.id) = 0, 0, pl.quantity
) FROM product_log pl
WHERE pl.id_store = :store AND pl.id_product = ps.id_product ORDER BY pl.date_log DESC, pl.id DESC LIMIT 1
)
);
将获取表中的第一个数量,而不是最后一个数量。您可以按如下方式执行条件检查
虽然我没有在Maria db中工作过,但我假设窗口功能在那里工作
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps
WHERE ps.id_localisation = :store
AND NOT EXISTS (select *
from (SELECT t.*,row_number() over(partition by t.id_product
,t.id_store
order by date_log desc) as rnk
FROM product_log t
)x
where x.rnk=1 /*gets you the latest entry in log*/
and x.id_product=ps.id_product /*same product*/
and x.id_store=ps.id_store /*same store*/
and x.quantity=ps.quantity /*and if its has same quantity*/
)
)
请参阅:我提供了一个指向SQL FIDLE的链接。谢谢你指出。为了这个例子,我放弃了FK,因为它们不是我这里真正的问题。我会通过跟踪先前的数量
,然后添加一个条件,其中。。。ps.quantity:数量
,以供选择。这将导致插入零行,除非数量与前一行不同。您的意思是要将一些列前一行数量
添加到表产品日志
?我不确定我是否准确地理解了你的意思。你检查INSERT中的id\u localization
列,该列在DDL中不存在。。。您希望参考上次记录的时间,而在示例数据中,您有3个重复项,分别为date\u log
列。。。还有更多的问题。请按顺序提问。窗口函数是在MariaDB 10.2.0中首次引入的。
INSERT INTO product_log(
SELECT NULL, ps.id_product, ps.id_store, ps.quantity, CURRENT_DATE()
FROM product_store ps
WHERE ps.id_localisation = :store
AND NOT EXISTS (select *
from (SELECT t.*,row_number() over(partition by t.id_product
,t.id_store
order by date_log desc) as rnk
FROM product_log t
)x
where x.rnk=1 /*gets you the latest entry in log*/
and x.id_product=ps.id_product /*same product*/
and x.id_store=ps.id_store /*same store*/
and x.quantity=ps.quantity /*and if its has same quantity*/
)
)