[英]increment a line according to the previous line
This is my table 这是我的桌子
id / step / piece / tube / Validate / tool / tool2 / tool3 / index
__________________________________________________________________
1 / step3 /4113292/ ezpz / ok /616255/222233/222231 / 0
2 / step6 /4113292/ ezpz / /333255/212393/922231 / 0
3 / step2 /3322234/ PZEZ / /321938/330200/3O32O / 0
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 0
I want to increment the index when I add a new step(with an insert) who has the same tube and piece. 当我添加具有相同管和零件的新步骤(带有插入物)时,我想增加索引。
for exemple: 举个例子:
id / step / piece / tube / Validate / tool / tool2 / tool3 / index
__________________________________________________________________
1 / step3 /4113292/ ezpz / ok /616255/222233/222231 / 1
2 / step6 /4113292/ ezpz / /333255/212393/922231 / 2
3 / step2 /3322234/ PZEZ / /321938/330200/3O32O / 1
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 3
and if I add a new step for piece 4113292 and tube ezpz I want this : 如果我为4113292和ezpz管添加一个新步骤,我想要这样:
id / step / piece / tube / Validate / tool / tool2 / tool3 / index
__________________________________________________________________
1 / step3 /4113292/ ezpz / ok /616255/222233/222231 / 1
2 / step6 /4113292/ ezpz / /333255/212393/922231 / 2
3 / step2 /3322234/ PZEZ / /321938/330200/3O32O / 1
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 3
5 / step7 /4113292/ ezpz / /616255/902233/8243231 / 4
or if I add a new step for the piece 3322234 或者如果我为零件3322234添加新步骤
id / step / piece / tube / Validate / tool / tool2 / tool3 / index
__________________________________________________________________
1 / step3 /4113292/ ezpz / ok /616255/222233/222231 / 1
2 / step6 /4113292/ ezpz / /333255/212393/922231 / 2
3 / step2 /3322234/ PZEZ / /321938/330200/3O32O / 1
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 3
5 / step1 /3322234/ PZEZ / /432938/330200/3O32O / 2
i do that cause when a step is ok I want to go to the next step 我这样做是因为当一个步骤确定时,我想转到下一步
Example insert from user - instead a "?" 用户插入的示例-而不是“?” put "0" as default value, because every first insert will be 0.
将“ 0”设置为默认值,因为每个第一次插入都将为0。
INSERT INTO operation (step, piece, tube, tool,tool2,3,index) VALUES (step3, 4113292, ezpz,616255,902233,8243231, 0);
When you do an insert again with same piece and tube, this trigger should do the job. 当您再次用相同的管件插入时,此扳机即可完成工作。
DELIMITER //
CREATE TRIGGER yourTrigger
BEFORE INSERT
ON yourTable FOR EACH ROW
BEGIN
DECLARE maxIndex INT DEFAULT -1;
SELECT MAX(index1) m
FROM yourTable
WHERE piece = NEW.piece
AND tube = NEW.tube
INTO maxIndex;
IF maxIndex >= 0 THEN
SET NEW.index1 = maxIndex + 1;
END IF;
END;//
DELIMITER;
Tested in SQL Fiddle . 在SQL Fiddle中测试。
PS I had to rename "index" to "index1" because SQL Fiddle doesn't work with column named "index". PS我必须将“ index”重命名为“ index1”,因为SQL Fiddle不适用于名为“ index”的列。
create a "data_event" table like this : 创建一个“ data_event”表,如下所示:
id / step / piece / tube / Validate / tool / tool2 / tool3 / index
_____________________________________________________________________
1 / step3 /4113292/ ezpz / ok /616255/222233/222231 / 1
2 / step6 /4113292/ ezpz / /333255/212393/922231 / 1
2 / step6 /4113292/ ezpz / /333255/212393/922231 / 1
3 / step2 /3322234/ PZEZ / /321938/330200/3O32O / 1
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 1
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 1
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 1
5 / step1 /3322234/ PZEZ / /432938/330200/3O32O / 1
5 / step1 /3322234/ PZEZ / /432938/330200/3O32O / 1
Use this table "table_event" for insert (or delete) data (always with index=1) 使用此表“ table_event”来插入(或删除)数据(总是使用index = 1)
create view data_aggregate as
select
id,step,piece,tube,Validate,tool,tool2,tool3,sum(index) as index
from
data_event
group by
id,step,piece,tube,Validate,tool,tool2,tool3
id / step / piece / tube / Validate / tool / tool2 / tool3 / index
______________________________________________________________________
1 / step3 /4113292/ ezpz / ok /616255/222233/222231 / 1
2 / step6 /4113292/ ezpz / /333255/212393/922231 / 2
3 / step2 /3322234/ PZEZ / /321938/330200/3O32O / 1
4 / step4 /4113292/ ezpz / /616255/902233/8243231 / 3
5 / step1 /3322234/ PZEZ / /432938/330200/3O32O / 2
Use this table for select 使用此表进行选择
Sometimes (may be every day, every hour...) you can replace the content of table_event by the result of the view (using a transaction and an intermediate table) to compress storage 有时(可能每天,每小时...)您可以用视图结果(使用事务和中间表)替换table_event的内容以压缩存储
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.