Reputation: 15292
I am creating one procedure for my regular repeated job.
Within this, there is one steps to insert multiple rows from one table into temporary 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
But it gives me error : Result consisted of more than one row
Upvotes: 1
Views: 134
Reputation: 175756
Correct INSERT SELECT
syntax:
INSERT INTO tmpUserList(id, user_type,first_name,last_name)
SELECT id, user_type,first_name,last_name
FROM user
WHERE id IN (usersId);
If usersId
contains multiple values you could use:
WHERE FIND_IN_SET(id, usersId); -- table scan
Related: MySQL Prepared statements with a variable size variable list
Upvotes: 1