2017-09-13 151 views
0

我已經做了兩個TableQuotation1Quotation2)和它具有以下Columns像:如何連接兩個表和第三個表顯示結果

CREATE TABLE Quotation1 (
    Q_No  INTEGER (10), 
    Date  DATE, 
    C_Code VARCHAR (45), 
    C_Name VARCHAR (45), 
    C_Contact VARCHAR (45), 
    G_Tot  VARCHAR (45), 
    Remarks VARCHAR (250) 
); 

CREATE TABLE Quotation2 (
    Q_No   VARCHAR (45), 
    P_Code  VARCHAR (45), 
    P_Name  VARCHAR (45), 
    Price  VARCHAR (45), 
    Qnty   INTEGER (10), 
    Amt   VARCHAR (45), 
    Discount_Per INTEGER (10), 
    Discount  INTEGER (10), 
    VAT_Per  INTEGER (10), 
    VAT   INTEGER (10), 
    Tot_Amt  VARCHAR (45) 
); 

我不得不Joins這兩個Table並顯示其ResultThird TableQuotation其中有以下Columns像:

CREATE TABLE Quotation (
    Q_No   INT (10)  PRIMARY KEY 
           NOT NULL, 
    Date   DATE, 
    C_Code  VARCHAR (45) REFERENCES CustomerInfo (C_Code) ON DELETE NO ACTION 
                   ON UPDATE NO ACTION 
                   MATCH SIMPLE NOT DEFERRABLE INITIALLY DEFERRED, 
    C_Name  VARCHAR (45), 
    C_Contact VARCHAR (45), 
    Remarks  VARCHAR (45), 
    P_Code  VARCHAR (45) REFERENCES ProductInfo (P_Code) ON DELETE NO ACTION 
                   ON UPDATE NO ACTION 
                   MATCH SIMPLE NOT DEFERRABLE INITIALLY DEFERRED, 
    P_Name  VARCHAR (45), 
    Price  VARCHAR (45), 
    Qnty   INTEGER (10), 
    Amount  VARCHAR (45), 
    Discount_Per INTEGER (10), 
    Discount  INTEGER (10), 
    VAT_Per  INTEGER (10), 
    VAT   INTEGER (10), 
    Tot_Amt  VARCHAR (45), 
    G_Tot  VARCHAR (45) 
) 
WITHOUT ROWID; 

我已經申請繼Query一樣:

INSERT INTO Quotation (Q_No,Date,C_Code,C_Name,C_Contact,Remarks,P_Code,P_Name,Price,Qnty,Amount,Discount_Per,Discount,VAT_Per,VAT,Tot_Amt,G_Tot) 
SELECT Quotation2.Q_No, 
     Quotation1.Date, 
     Quotation1.C_Code, 
     Quotation1.C_Name, 
     Quotation1.C_Contact, 
     Quotation1.Remarks, 
     Quotation2.P_Code, 
     Quotation2.P_Name, 
     Quotation2.Price, 
     Quotation2.Qnty, 
     Quotation2.Amt, 
     Quotation2.Discount_Per, 
     Quotation2.Discount, 
     Quotation2.VAT_Per, 
     Quotation2.VAT, 
     Quotation2.Tot_Amt, 
     Quotation1.G_Tot 
FROM Quotation1, 
    Quotation2 
INNER JOIN Quotation ON Quotation1.Q_No=Quotation2.Q_No; 

它給人的關注Result我愛:[02:16:23] Query finished in 0.010 second(s).

但不顯示任何Data甚至沒有Column Name請幫助。

+1

對於初學者..你不應該使用'Date'命名一個字段。這只是一個壞習慣,因爲它是MySQL使用的保留術語。我懷疑它會影響你的查詢,只是指出一個觀察... – Zak

回答

0

爲什麼在插入時加入目標表?

-- Insert 
INSERT INTO Quotation (Q_No,Date,C_Code,C_Name,C_Contact,Remarks,P_Code,P_Name,Price,Qnty,Amount,Discount_Per,Discount,VAT_Per,VAT,Tot_Amt,G_Tot) 
SELECT Quotation2.Q_No, 
     Quotation1.Date, 
     Quotation1.C_Code, 
     Quotation1.C_Name, 
     Quotation1.C_Contact, 
     Quotation1.Remarks, 
     Quotation2.P_Code, 
     Quotation2.P_Name, 
     Quotation2.Price, 
     Quotation2.Qnty, 
     Quotation2.Amt, 
     Quotation2.Discount_Per, 
     Quotation2.Discount, 
     Quotation2.VAT_Per, 
     Quotation2.VAT, 
     Quotation2.Tot_Amt, 
     Quotation1.G_Tot 
FROM Quotation1 
JOIN Quotation2 ON Quotation1.Q_No = Quotation2.Q_No; 

-- Show results: 
SELECT * FROM Quotation; 

應該做的工作。

+0

[20:54:09]對數據庫'InventorySystem'執行SQL查詢時出錯:UNIQUE約束失敗:Quotation.Q_No –

+0

這告訴你,你在Quotation1或Quotation2中重複了Q_No。很可能是因爲你沒有你的約束。你可以通過使用INSERT IGNORE而不是INSERT來消除這個錯誤**但是要小心,因爲這樣做會導致數據丟失。 –

+0

感謝它的工作,我已經做了''獨特的衝突IGNORE''的改變# –

相關問題