mysql 过程:结果包含多行 select 语句
mysql procedure : Result consisted of more than one row with select statement
我正在为我的常规重复工作创建一个程序。
其中,有一个步骤可以将一个 table 中的多行插入临时 table。
CREATE TABLE `tmpUserList` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_type` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
..... some more queries.
INSERT INTO tmpUserList (
SELECT id, user_type,first_name,last_name, from user where id in (usersId)
);
SELECT * FROM tmpUserList; // return the result
但它给我错误:结果包含不止一行
更正 INSERT SELECT
语法:
INSERT INTO tmpUserList(id, user_type,first_name,last_name)
SELECT id, user_type,first_name,last_name
FROM user
WHERE id IN (usersId);
如果 usersId
包含多个值,您可以使用:
WHERE FIND_IN_SET(id, usersId); -- table scan
相关:MySQL Prepared statements with a variable size variable list
我正在为我的常规重复工作创建一个程序。
其中,有一个步骤可以将一个 table 中的多行插入临时 table。
CREATE TABLE `tmpUserList` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_type` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
..... some more queries.
INSERT INTO tmpUserList (
SELECT id, user_type,first_name,last_name, from user where id in (usersId)
);
SELECT * FROM tmpUserList; // return the result
但它给我错误:结果包含不止一行
更正 INSERT SELECT
语法:
INSERT INTO tmpUserList(id, user_type,first_name,last_name)
SELECT id, user_type,first_name,last_name
FROM user
WHERE id IN (usersId);
如果 usersId
包含多个值,您可以使用:
WHERE FIND_IN_SET(id, usersId); -- table scan
相关:MySQL Prepared statements with a variable size variable list