简体   繁体   English

在1个查询mysql中插入多行

[英]Inserting multiple rows in 1 query mysql

I've searched about this already on stackoverflow and found this: Inserting multiple rows in mysql 我已经在stackoverflow上搜索了此内容,并发现了这一点: 在mysql中插入多行

Sadly when I try this it's not working for me. 可悲的是,当我尝试此操作时,它对我不起作用。

when I got a query like this: 当我收到这样的查询时:

INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0);

it gives me the following error: 它给了我以下错误:

#1136 - Column count doesn't match value count at row 1

even when I add the () arround my values like this: 即使当我添加()围绕我的值时,如下所示:

INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
(('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0));

it gives me the following error: 它给了我以下错误:

#1241 - Operand should contain 1 column(s) 

So how can I solve this problem and add multiple lines with 1 query? 那么,如何解决这个问题并通过1条查询添加多行呢?

Change to: 改成:

INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', 'test1', 1,0,0),
('2016-01-12', 'test2',2,0,0);

You have to add the values row wise. 您必须逐行添加值。

you insert 2 values in more than 2 column, your query can be: 您在2列以上插入2个值,您的查询可以是:

INSERT INTO productielijn1
(Datum, Productieomschrijving)
VALUES
(('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0));

or: 要么:

INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
(('2016-01-12', '2016-01-12', '', '', ''),
('test1', 'test2', '', '', ''),
(1, 2, '', '', ''),
(0, 0, '', '', ''),
(0, 0, '', '', ''));

In your query: 在您的查询中:

INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12'),
('test1', 'test2'),
(1, 2),
(0, 0),
(0, 0);

Fields list has 5 columns where as your values list has only two. 字段列表有5列,其中值列表只有2列。

Either remove three from fields list or add three in the columns list. 从字段列表中删除三个,或在列列表中添加三个。

Solution to this: 解决方案:

Add default values to three remaining value columns: 将默认值添加到三个剩余值列:

INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12', '', '', ''),
('test1', 'test2', '', '', ''),
(1, 2, '', '', ''),
(0, 0, '', '', ''),
(0, 0, '', '', ''),;

Each set of values should match the number of the columns, so if the remaining 3 fields are supposed to remain empty, just add those to the sets, or fill them up with corresponding values: 每组值都应与列数匹配,因此,如果应该将其余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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM