1

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

iminiki
  • 2,363
  • 11
  • 33
  • 43
RIYAJ KHAN
  • 14,597
  • 5
  • 30
  • 52

1 Answers1

1

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

Lukasz Szozda
  • 139,860
  • 19
  • 198
  • 228