[英]MySql - Joining another table with multiple rows, inserting a query into a another query?
[英]Inserting multiple rows in 1 query mysql
我已經在stackoverflow上搜索了此內容,並發現了這一點: 在mysql中插入多行
可悲的是,當我嘗試此操作時,它對我不起作用。
當我收到這樣的查詢時:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0);
它給了我以下錯誤:
#1136 - Column count doesn't match value count at row 1
即使當我添加()圍繞我的值時,如下所示:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
(('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0));
它給了我以下錯誤:
#1241 - Operand should contain 1 column(s)
那么,如何解決這個問題並通過1條查詢添加多行呢?
改成:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', 'test1', 1,0,0),
('2016-01-12', 'test2',2,0,0);
您必須逐行添加值。
您在2列以上插入2個值,您的查詢可以是:
INSERT INTO productielijn1
(Datum, Productieomschrijving)
VALUES
(('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0));
要么:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
(('2016-01-12', '2016-01-12', '', '', ''),
('test1', 'test2', '', '', ''),
(1, 2, '', '', ''),
(0, 0, '', '', ''),
(0, 0, '', '', ''));
在您的查詢中:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0);
字段列表有5列,其中值列表只有2列。
從字段列表中刪除三個,或在列列表中添加三個。
解決方案:
將默認值添加到三個剩余值列:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12', '', '', ''),
('test1', 'test2', '', '', ''),
(1, 2, '', '', ''),
(0, 0, '', '', ''),
(0, 0, '', '', ''),;
每組值都應與列數匹配,因此,如果應該將其余3個字段保留為空,則只需將這些字段添加到組中,或用相應的值填充它們:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12','','',''),
('test1', 'test2','','',''),
(1, 2,'','',''),
(0, 0,'','',''),
(0, 0,'','','');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.