0

This is a MySQL 5.5 DB. You're supposed to be able to insert multiple rows of values with this syntax:

INSERT INTO tbl_name (a,b,c)
VALUES(1,2,3), (4,5,6), (7,8,9);

But I'm getting an error ("Column count doesn't match value count at row 1") on the following query:

INSERT INTO users_X_shareItems (userID, itemID, userAction, detail, actionDate)
VALUES ('CB381FC5-6373-4D01-A2ED-01CEACFA750B'), 
    ('16nhbfsg6apltgtfhjkb29z4w'),
    ('like'),
    (''),
    (NULL)

Are my counting skills deficient, or are there five columns' worth of values right there? In this instance there's only one row's worth of data; hence only one value in each set of parentheses. But the PHP function that builds this query takes an arbitrary number of rows' worth of data, and that's a functional requirement.

PLEASE NOTE in the example at the top, directly from the MySQL doc, the parentheses supposedly tell the engine that these are LISTS of values. Let's take that example and modify for an instance in which you're only adding ONE row's worth of values:

INSERT INTO tbl_name (a,b,c)
VALUES(1), (4), (7);

The wording of the documentation is ambiguous, so I'm going to reorganize the query as some have suggested.

8
  • try this: INSERT INTO users_X_shareItems (userID, itemID, userAction, detail, actionDate) VALUES ('CB381FC5-6373-4D01-A2ED-01CEACFA750B', '16nhbfsg6apltgtfhjkb29z4w', 'like', '', NULL) Jul 13, 2014 at 9:20
  • Are you sure that there are no left columns that are not mentioned in the query and that they are not accepting nulls or default values by default? Jul 13, 2014 at 9:20
  • Thanks puredevotion, but that is not valid multiple-row syntax. That will insert only row (which is all that's needed in this one instance, but the function that builds this query takes an arbitrary number of rows).
    – Oscar
    Jul 13, 2014 at 9:23
  • 2
    Your syntax in saying: Insert into 5 columns 5 rows with one column :). You'll have to learn basic SQL syntax.
    – elrado
    Jul 13, 2014 at 9:28
  • 1
    @Oscar each set of parenthesis represents 1 row and each comma separated value inside a set of parentehsis represents 1 column. in your example you have 5 sets of parenthesis (i.e 5 rows) with 1 value each (i.e. 1 column)
    – FuzzyTree
    Jul 13, 2014 at 9:38

3 Answers 3

2

From the different comments I suspect you're making the assumption that the INSERT statement works like a function that accepts variable arguments (such as COALESCE() or CONCAT_WS()). That's simply not the case: is isn't a function and you need the same item count on each list:

INSERT INTO foo (a) VALUES
(?),
(?),
(?),
(?),
(?);
INSERT INTO foo (a, b) VALUES
(?, ?),
(?, ?),
(?, ?),
(?, ?),
(?, ?);
INSERT INTO foo (a, b, c) VALUES
(?, ?, ?),
(?, ?, ?),
(?, ?, ?),
(?, ?, ?),
(?, ?, ?);

... but never:

-- Not valid
INSERT INTO foo (a, b) VALUES
(?),
(?, ?),
(?, ?, ?);

If the table design allows so, some of the actual values can be NULL, but it isn't possible to omit them entirely.

If you need to handle different column counts, you'll have to build your SQL code dynamically. That's trivial in most programming languages.


Just seen your edit. You misunderstood the multiple-row syntax. It isn't like this:

-- Not valid
INSERT INTO person (name, age) values
('Abe', 'Bill', 'Charles'),
(23, 45, 17);

It's like this:

INSERT INTO person (name, age) values
('Abe', 23),
('Bill', 45),
('Charles', 17);
1
  • Thanks. The wording of the doc doesn't make it clear which direction the lists are supposed to go. Your "not valid" example is indeed the way I interpreted it.
    – Oscar
    Jul 13, 2014 at 10:01
1

Why do you close the VALUES brackets after each single value? SQL know asumes you like to add 5 different rows with every row containing only one value but you said you will provide five values. So your Query should be

INSERT INTO users_X_shareItems (userID, itemID, userAction, detail, actionDate) VALUES
('CB381FC5-6373-4D01-A2ED-01CEACFA750B', '16nhbfsg6apltgtfhjkb29z4w', 'like', '', NULL);

Edit If you want to add more rows in one query you still have to provide these 5 values. See this example

INSERT INTO users_X_shareItems (userID, itemID, userAction, detail, actionDate) VALUES
    ('CB381FC5-6373-4D01-A2ED-01CEACFA750B', '16nhbfsg6apltgtfhjkb29z4w', 'like', '', NULL),
    ('row2', 'row2', 'like', '', NULL),
    ('row3', 'row3', 'like', '', NULL);
2
  • They're enclosed that way because this is required for multi-row syntax (as in the example I provided, from the MySQL doc). In this example I'm only adding one row, but the function takes an arbitrary number of rows' worth of data. On one trip through, I might add five rows. The next time, only one. Putting the values in separate parentheses is supposed to tell the SQL engine that these are lists of values for each column, to be used in multiple rows. A list with one entry is still valid.
    – Oscar
    Jul 13, 2014 at 9:30
  • Thats not true. If you provide a list of columns you like to fill you have to provide the same amount of data inside a VALUES list. You have to change your column list if you like to insert less data.
    – Tom
    Jul 13, 2014 at 9:32
1

The correct syntax:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

However in your case, you made it like this:

INSERT INTO tbl_name (a,b,c,d,e) VALUES('1'),('2'),('3'),('4'),('5');

The total of items in the brackets must be the same with the total of columns, you inserted only 1 value for each row, that caused the mismatch error.

Just change it to:

INSERT INTO users_X_shareItems (userID, itemID, userAction, detail, actionDate) VALUES ('CB381FC5-6373-4D01-A2ED-01CEACFA750B','16nhbfsg6apltgtfhjkb29z4w','like','', NULL)
5
  • That's not multi-row syntax. The syntax you suggested does not conform to the correct example at the top. Remember, I'm only inserting one row THIS time; but that may not be true on the next pass through. Enclosing each column's values in parentheses tells SQL that it's a list; in this case the list is only one entry long.
    – Oscar
    Jul 13, 2014 at 9:28
  • INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. That is what is written in the doc. Your syntax is incorrect, because you enclose only 1 value within the parentheses, while your table itself has 5 columns.
    – Jay Kazama
    Jul 13, 2014 at 9:44
  • While this syntax may be wrong, the doc is not clear on which direction the lists go. "Column values" may be interpreted to mean lists of values for THAT column.
    – Oscar
    Jul 13, 2014 at 9:47
  • @Oscar - Borth wording and example could be improved, but there's an example of invalid code right after that paragraph that implies so. Jul 13, 2014 at 9:50
  • Actually, that invalid example doesn't resemble mine either. It lacks parentheses for each column.
    – Oscar
    Jul 13, 2014 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.