Tim
Tim

Reputation: 37

Very basic MySQL code not working

I'm struggling with some very basic MySQL code, so I'm clearly misunderstanding something very fundamental. This super simple code is a good example:

CREATE TEMPORARY TABLE temp_table (count int);
INSERT INTO temp_table VALUES((1),(2),(3));
SELECT * FROM temp_table;

This returns:

11 errors were found during analysis.

A comma or a closing bracket was expected (near "1" at position 31)
Unexpected token. (near ")" at position 32)
Unexpected token. (near "," at position 33)
Unexpected token. (near "(" at position 34)
Unexpected token. (near "2" at position 35)
Unexpected token. (near ")" at position 36)
Unexpected token. (near "," at position 37)
Unexpected token. (near "(" at position 38)
Unexpected token. (near "3" at position 39)
Unexpected token. (near ")" at position 40)
Unexpected token. (near ")" at position 41)

It works just fine if I only do INSERT INTO temp_table VALUES(1); But it seems to take issue with me using parentheses, so I can't seem to insert multiple values.

If I do INSERT INTO temp_table VALUES(1,2,3); It returns #1136 - Column count doesn't match value count at row 1

And I'm running into issues like this again and again with various MySQL code examples that I'm finding in various sources, including StackOverflow. So I'm not sure what I'm missing.

In my hosts control panel, I can see that I'm running MariaDB 5.3 and in phpMyAdmin (4.5.2), I can see that I'm running MySQL 5.7.9.

And yet I run into issues running code that comes directly from MariaDB's website.

Would you mind (gently) cluing me in? I'm sure this must be basic, and I'm willing to learn. I just don't know what the problem is, and until I learn these fundamentals, I'm just going to be spinning my wheels.

Upvotes: 2

Views: 3026

Answers (2)

MSameer
MSameer

Reputation: 443

Elaborating on @scaisEdge note, when you write insert query with multiple values, you cannot group all values together.

 //Wrong
 INSERT INTO temp_table values ((val1),(val2),(val3));

You should do the following instead:

//Right
INSERT INTO temp_table values (val1), (val2), (val3);

Hence your query should be:

INSERT INTO temp_table VALUES(1),(2),(3);

Hope this explains, enjoy!

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133380

Remove wrapping ()

CREATE TEMPORARY TABLE temp_table (count int);
INSERT INTO temp_table VALUES(1),(2),(3);
SELECT * FROM temp_table;

Upvotes: 3

Related Questions