简体   繁体   English

需要创建MySQL过程以根据条件从表中获取数据并将其插入另一个表中

[英]Need to Create MySQL Procedure for getting data from table based on condition and insert them into another table

Using a single MySQL procedure i need to write queries to get data from a table (where the results can be of a list also) after getting the results,need to insert those selected rows into another table of the same database..I'm finding dificulty in getting the result of the select query and fetching values to insert into another table... 使用单个MySQL过程我需要在获取结果后编写查询以从表中获取数据(结果也可以是列表),需要将这些选定的行插入到同一数据库的另一个表中。我是在获取select查询的结果并获取要插入到另一个表中的值时找到困难...

Iam able to do the above one if it returns only one row but in my case it can return any number of rows... 如果它只返回一行,我可以做上面的那一行,但在我的情况下,它可以返回任意数量的行...

DELIMITER $$ 

USE `scmn_nov21`$$

DROP PROCEDURE IF EXISTS `procedure1`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `procedure1`(
  IN Param1 VARCHAR(255),
  OUT Param2 VARCHAR(255),
  OUT Param3 VARCHAR(255)
)
BEGIN
  DECLARE myvar TEXT;

  SET myvar = (SELECT column1 FROM table1 WHERE column1 =2);

  INSERT INTO table1 (column1,column2,column3)
  VALUES (myvar,'Malaysia','Asia');

  COMMIT;
END$$

DELIMITER ;

I believe that you can do a Create as select: http://dev.mysql.com/doc/refman/5.7/en/create-table-select.html 我相信您可以选择Create as: http//dev.mysql.com/doc/refman/5.7/en/create-table-select.html

Returning malassia and asia as fixed values for your query... 将malassia和asia作为查询的固定值返回...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM