创建一个从phpMyAdmin中的另一个表中提取信息的触发器

时间:2015-12-03 09:08:12

标签: mysql sql triggers

我正在尝试创建一个相当复杂的触发器,我不确定它是否可以在phpMyAdmin上完成。

现在我有了这个查询,它创建了一个包含我需要的所有信息的表。

CREATE TABLE SeniorDB_Shipping

SELECT

    SeniorDB_Invoice.ID_Order, 
    SeniorDB_Customer.MCT_Code,
    SeniorDB_Customer.Customer_Name,
    SeniorDB_Customer.Customer_Address,
    SeniorDB_Customer.Customer_City,
    SeniorDB_Customer.Customer_State,
    SeniorDB_Invoice.Shipping_Company

FROM SeniorDB_Customer

Join SeniorDB_Invoice ON SeniorDB_Customer.MCT_Code = SeniorDB_Invoice.MCT_Code

Inserted into this table

正如您在图像中看到的,当我运行查询时,它会从信息上方的表中提取信息。我正在尝试(并且失败)创建一个触发器来执行同样的操作,而不必每次都create一个全新的表。我看到的所有其他帖子与creating表格相似,而不是inserting表格。

触发器的作用是,当我输入ID_Order时,其余信息将从数据库中提取。

这是我到目前为止的触发器:

    delimiter ~

        create trigger SeniorDB_Shipping before insert on SeniorDB_Shipping
        for each row begin
            set new.SeniorDB_Shipping.MCT_Code =            new.SeniorDB_Customer.MCT_Code,;
            set new.SeniorDB_Shipping.Customer_Name =       new.SeniorDB_Customer.Customer_Name,;
            set new.SeniorDB_Shipping.Customer_Address =    new.SeniorDB_Customer.Customer_Address,;
            set new.SeniorDB_Shipping.Customer_City =       new.SeniorDB_Customer.Customer_City,;
            set new.SeniorDB_Shipping.Customer_State =      new.SeniorDB_Customer.Customer_State,;
            set new.SeniorDB_Shipping.Shipping_Company =    new.SeniorDB_Customer.Shipping_Company,;
        end~

    delimiter ;

我觉得我就在那里。当我输入ID_Order时,我无法将其链接到。

如果您想查看数据库,请使用此页面:http://polonium.forest.usf.edu/~sngamwon/SeniorProject/SeniorDB_Order.php

1 个答案:

答案 0 :(得分:0)

好的,所以你需要运行一次:

/* Create the table with a primary key */
create table `SeniorDB_Shipping` (
    `id` INT unsigned AUTO_INCREMENT NOT NULL,
    primary key(id),
    `ID_Order` int NOT NULL,
    `MCT_Code` int NOT NULL,
    `Customer_Name` varchar(255) NOT NULL,
    `Customer_Address` varchar(255) NOT NULL,
    `Customer_City` varchar(255) NOT NULL,
    `Customer_State` varchar(255) NOT NULL,
    `Shipping_Company` varchar(255) NOT NULL
) ENGINE=MyISAM CHARACTER SET=utf8 COLLATE utf8_general_ci;

然后你可以运行

/* Insert statement */
INSERT INTO `SeniorDB_Shipping` (
    `ID_Order`, 
    `MCT_Code`, 
    `Customer_Name`, 
    `Customer_Address`, 
    `Customer_City`,
    `Customer_State`,
    `Shipping_Company`
) SELECT
    invoice.ID_Order, 
    customer.MCT_Code,
    customer.Customer_Name,
    customer.Customer_Address,
    customer.Customer_City,
    customer.Customer_State,
    invoice.Shipping_Company
FROM 
    SeniorDB_Customer as customer
    Join SeniorDB_Invoice as invoice
        ON customer.MCT_Code = invoice.MCT_Code;

我在我自己的PHPMyAdmin中运行它,所以它可以工作。但我显然没有你的架构。已知问题:

  • 每次使用 ALL 两个表中的数据填充SeniorDB_Shipping。根据需要修改查询,以便仅选择最近的数据,如果这不是您想要的。如果ID_Order是主键,您可以检查它是否已经存在。