Reputation: 163
I've searched about this already on stackoverflow and found this: Inserting multiple rows in 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?
Upvotes: 2
Views: 62
Reputation: 9430
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:
INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12','','',''),
('test1', 'test2','','',''),
(1, 2,'','',''),
(0, 0,'','',''),
(0, 0,'','','');
Upvotes: 0
Reputation: 23958
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.
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, '', '', ''),;
Upvotes: 0
Reputation: 69440
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.
Upvotes: 3
Reputation: 7023
you insert 2 values in more than 2 column, your query can be:
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, '', '', ''));
Upvotes: 1