MySQL:单列的多个插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3356317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: Multiple Inserts for a single column
提问by tsgrasser
I'm looking for a way to do multiple row inserts when I'm only inserting data for a single column.
当我只为单列插入数据时,我正在寻找一种方法来进行多行插入。
Here is the example table:
这是示例表:
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | tinyint(4) | NO | PRI | NULL | auto_increment |
| name | varchar(40) | NO | UNI | NULL | |
+-------+-------------+------+-----+---------+----------------+
I want to be able to insert something like ('admin', 'author', 'mod', 'user', 'guest') into the name column for each row.
我希望能够在每行的名称列中插入类似 ('admin', 'author', 'mod', 'user', 'guest') 的内容。
The MySQL documentation shows that multiple inserts should be in the format:
MySQL 文档显示多个插入应采用以下格式:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
However my statement ends up looking like this:
但是我的声明最终看起来像这样:
INSERT INTO User_Role(name) VALUES ('admin','author','mod','user','guest');
And I get the following:
ERROR 1136 (21S01): Column count doesn't match value count at row 1
我得到以下信息:
错误 1136 (21S01):列计数与第 1 行的值计数不匹配
Meaning that it thinks I'm trying to do a single row insert.
这意味着它认为我正在尝试进行单行插入。
I'm not sure if I'm just missing something simple here, but I don't see anything in particular in the MySQL docs for this use case.
我不确定我是否只是在这里遗漏了一些简单的东西,但我在 MySQL 文档中没有看到任何关于这个用例的特别内容。
回答by Scott M.
your syntax is a bit off. put parentheses around each data "set" (meaning a single value in this case) that you are trying to insert.
你的语法有点不对。在您尝试插入的每个数据“集”(在这种情况下表示单个值)周围加上括号。
INSERT INTO User_Roll(name) VALUES ('admin'), ('author'), ('mod'), ('user'), ('guest');
回答by Pooja Khatri
I will advise you Don't put multiple values in a column. make a new table:
我会建议您不要在一列中放置多个值。创建一个新表:
INSERT INTO table_name (id, name) VALUES (1, 'name1'), (1, 'name2'), (1, 'name3'), (1, 'name4');