3

I am just learning MySQL and am trying to create a basic table. But I am getting this error:

Error Code: 1136. Column count doesn't match value count at row 1.

Query is

insert into user(username,password,email,created,last_updated) values (
    ('TEST USERNAME','TEST PASSWORD','Test@test.com',current_timestamp(),current_timestamp()), 
    ('TEST USERNAME 2','TEST PASSWORD 2','Test2@test.com',current_timestamp(),current_timestamp())
);

The columns in the table are :

  • id
  • username
  • password
  • email
  • created
  • last_updated
4

2 回答 2

4

删除括号Values(..),它应该是Values (..), (..)

insert into user(username,password,email,created,last_updated) 
values
('TEST USERNAME','TEST PASSWORD','Test@test.com',current_timestamp(),current_timestamp()), 
('TEST USERNAME 2','TEST PASSWORD 2','Test2@test.com',current_timestamp(),current_timestamp());

Docs,语法是:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    {VALUES | VALUE} (value_list) [, (value_list)] ...
    [ON DUPLICATE KEY UPDATE assignment_list]

...

使用 VALUES 语法的 INSERT 语句可以插入多行。为此,请包含多个以逗号分隔的列值列表,列表用括号括起来并用逗号分隔。例子:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
于 2018-11-04T05:07:17.233 回答
0

你表中的列有 id 但你写的时候没有

insert into user(username,password,email,created,last_updated)

你应该写

insert into user(id,username,password,email,created,last_updated)
于 2019-07-31T11:27:34.463 回答