2

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?

Community
  • 1
  • 1
Bart88
  • 163
  • 1
  • 2
  • 10
  • Seems pretty self-explanatory. You specified 5 columns but only supplied 2 values per row. – shmosel Jan 12 '16 at 08:59
  • Oh right, it works the other way arround... I thought I needed to add each value 2 times (if I wanted to add 2 lines). My query isn't build up correctly. – Bart88 Jan 12 '16 at 09:01

4 Answers4

3

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.

Jens
  • 67,715
  • 15
  • 98
  • 113
1

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, '', '', ''));
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
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.

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, '', '', ''),;
Pupil
  • 23,834
  • 6
  • 44
  • 66
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:

INSERT INTO productielijn1
(Datum, Productieomschrijving, Aantal, AantalGemaakt, Verschil)
VALUES
('2016-01-12', '2016-01-12','','',''),
('test1', 'test2','','',''),
(1, 2,'','',''),
(0, 0,'','',''),
(0, 0,'','','');
n-dru
  • 9,285
  • 2
  • 29
  • 42