插入不在“报告”中的'report_temp'行?

时间:2010-07-09 15:24:15

标签: mysql inner-join

我有两张桌子:

report (reportID, VendorName, VendorID, MfgDate, PurchaseDate, etc.,)

report表中,reportID是主键。

report_temp与<{1}}具有相同的列,但没有任何约束。

我要report reportID不一样。 我写的是

insert the rows from report_temp to report

我还在 INSERT INTO report(reportID, VendorName, VendorID, MfgDate, PurchaseDate,...) NOT (SELECT reportID, VendorName, VendorID, MfgDate, PurchaseDate,... FROM report INNER JOIN report_temp USING (reportID, VendorName, VendorID, MfgDate,PurchaseDate,...)) 条款中尝试了reportID,但我无法理解...... 如果您尝试过这样的任何内容与我分享..

1 个答案:

答案 0 :(得分:0)

这将为您提供两个表中存在的一组reportID:

SELECT report_temp.reportID FROM report_temp, report WHERE report_temp.reportID=report.reportID

因此,这将为您提供要插入的行:

SELECT * FROM report_temp WHERE reportID NOT IN (SELECT report_temp.reportID FROM report_temp, report WHERE report_temp.reportID=report.reportID)

然后将其放入report表中:

INSERT INTO report SELECT * FROM report_temp WHERE reportID NOT IN (SELECT report_temp.reportID FROM report_temp, report WHERE report_temp.reportID=report.reportID)