通过使用触发器比较两个表的值来更新第三个表

发布于 2024-10-10 13:14:51 字数 166 浏览 2 评论 0原文

嗨,

我有三个表(Table1、Table2、Table3)。所有三个表都有相同的列名称(Company、License_count)。当 table2 中发生更新时,触发器将比较 table2 和 table2 的值。表1并将差异插入表三。我只需要对单行执行此操作。

有人可以帮忙吗?

Hii

I have three tables (Table1, Table2, Table3).All the three tables have same column names(Company, License_count). when an update will happen in table2, trigger will compare the values of table2 & table1 and insert the difference into table three. I have to do this only for single row.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2024-10-17 13:14:51

好吧,我明白了。

drop trigger if exists table1and2difference;
CREATE TRIGGER table1and2difference
AFTER UPDATE ON Table2
FOR EACH ROW
  INSERT INTO Table3(Company, License_count)values
    (new.Company, (SELECT (SUM(License_count1) - License_count2) as License_count FROM
                   (SELECT Company as company1, License_count as License_count1 FROM 
                           Table1 WHERE Company=new.Company) AS t1
                   INNER JOIN
                   (SELECT Company as company2, License_count as License_count2 FROM
                           Table2 WHERE Company=new.Company) As t2
                   On company1=company2));

作为参考,这些是我用来测试触发器的值。

create table Table1(
Company VARCHAR(100) NOT NULL,
License_count INT NOT NULL);

insert into Table1(Company,License_count)values('Test_company',10);

create table Table2(
Company VARCHAR(100) NOT NULL,
License_count INT NOT NULL);

insert into Table2(Company,License_count)values('Test_company',8);

create table Table3(
Company VARCHAR(100) NOT NULL,
License_count INT NOT NULL);

drop trigger if exists table1and2difference;
CREATE TRIGGER table1and2difference
AFTER UPDATE ON Table2
FOR EACH ROW
     INSERT INTO Table3(Company, License_count)values
         (new.Company, (SELECT (SUM(License_count1) - License_count2) as License_count FROM
                            (SELECT Company as company1, License_count as License_count1 FROM Table1 WHERE Company=new.Company) AS t1
                            INNER JOIN
                            (SELECT Company as company2, License_count as License_count2 FROM Table2 WHERE Company=new.Company) As t2
                            On company1=company2));

update Table2 set License_count=7;

Alright, I got it.

drop trigger if exists table1and2difference;
CREATE TRIGGER table1and2difference
AFTER UPDATE ON Table2
FOR EACH ROW
  INSERT INTO Table3(Company, License_count)values
    (new.Company, (SELECT (SUM(License_count1) - License_count2) as License_count FROM
                   (SELECT Company as company1, License_count as License_count1 FROM 
                           Table1 WHERE Company=new.Company) AS t1
                   INNER JOIN
                   (SELECT Company as company2, License_count as License_count2 FROM
                           Table2 WHERE Company=new.Company) As t2
                   On company1=company2));

For reference, these are the values I used to test the trigger.

create table Table1(
Company VARCHAR(100) NOT NULL,
License_count INT NOT NULL);

insert into Table1(Company,License_count)values('Test_company',10);

create table Table2(
Company VARCHAR(100) NOT NULL,
License_count INT NOT NULL);

insert into Table2(Company,License_count)values('Test_company',8);

create table Table3(
Company VARCHAR(100) NOT NULL,
License_count INT NOT NULL);

drop trigger if exists table1and2difference;
CREATE TRIGGER table1and2difference
AFTER UPDATE ON Table2
FOR EACH ROW
     INSERT INTO Table3(Company, License_count)values
         (new.Company, (SELECT (SUM(License_count1) - License_count2) as License_count FROM
                            (SELECT Company as company1, License_count as License_count1 FROM Table1 WHERE Company=new.Company) AS t1
                            INNER JOIN
                            (SELECT Company as company2, License_count as License_count2 FROM Table2 WHERE Company=new.Company) As t2
                            On company1=company2));

update Table2 set License_count=7;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文