2015-12-03 112 views
0

我試圖創建一個相當複雜的觸發器,我不確定它是否可以在phpMyAdmin上完成。創建一個觸發器,從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一張全新的表格。我所看到的所有其他帖子都是類似的,而不是inserting表格creating

觸發器的作用是,當我輸入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

+0

要澄清,第一個查詢不會創建一個表,並且您每次都需要一個新表?如果是這樣,是否有任何理由爲新表? – thelastshadow

+0

我的歉意,我改變了。我早些時候嘗試過'INSERT',但是我收到了很多錯誤。它作爲一個「CREATE TABLE」而不是「INSERT」。我假設我的語法或理解有點關閉 – user5619474

+0

我可以給你正確的查詢來將插入運行到你預先創建的同一個表中。我認爲你根本不需要觸發器。那是你的追求? – thelastshadow

回答

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中運行了它,所以它可以工作。但我顯然沒有你的架構。已知問題:

  • 這將填充SeniorDB_Shipping,每次ALL將數據從你的兩個表。根據需要修改查詢,只選擇最近的數據,如果這不是你想要的。如果ID_Order是主鍵,您可以檢查它是否已經存在。
+0

我不知道如何測試這個數據。我是否應該輸入一個ID_Order號碼,並且它會提取其餘的信息? – user5619474

+0

它應該創建一個新的'SeniorDB_Shipping'表,您可以在PHPMyAdmin中查看。一旦你運行第二個查詢,它會在'SeniorDB_Customer'中的每一行中爲該表添加一行。你可以查看'SeniorDB_Shipping'並確保它有你想要的。如果稍後要從另外兩個表中將單個新行加載到該表中,請運行相同的語句(第二個查詢),但在末尾添加'where invoice.ID_Order = N',其中N是要添加的發票ID 。 – thelastshadow

相關問題