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,549
  • 12
  • 35
  • 45
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53

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
  • 162,964
  • 23
  • 234
  • 275