![](/img/trans.png)
[英]how to update value in one column and automaticaly updated another column at different table laravel
[英]Update another table when another one is updated
我还不太清楚该怎么做,所以我正在做的是在线游戏。当用户购买新车时,我会进行插入:
$c->query("INSERT INTO user_cars (carid, userid, acc, speed) VALUES ('$buyid','$id','$acc','$speed')");
现在我有另一个表,我需要在上面的查询之后的上面的查询之后插入信息。我需要的是carid
用户可以拥有2辆以上的汽车,我现在该怎么办?
您有多种选择:
这是您要创建的触发器的基本演示。 出于说明目的,我还将ddl和示例插入到您的user_cars表中,以显示另一个表(我称为“ your_other_table”)收到插入到user_cars表中的插入(只是carid值)。
小提琴: http ://sqlfiddle.com/#!9/f76a7/1/0
(请注意,尽管“ your_other_tabe”本身没有直接插入,但如何与插入到“ user_cars”中的龋齿保持一排)
delimiter //
create table user_cars
(
carid int,
userid int,
acc int,
speed int,
constraint id_pk primary key (carid, userid)
)//
create table your_other_table
(
carid int
)//
create trigger your_trigger_name before insert on user_cars
for each row begin
insert into your_other_table (carid)
values (new.carid);
end
//
insert into user_cars values
(1, 2, 3, 99)//
delimiter ;
select *
from your_other_table;
输出:
| CARID |
|-------|
| 1 |
这是上述sql中创建触发器的唯一部分:
delimiter //
create trigger your_trigger_name before insert on user_cars
for each row begin
insert into your_other_table (carid)
values (new.carid);
end
//
delimiter ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.