如何将此Mysql语句转换为存储过程

时间:2012-06-29 06:00:13

标签: mysql stored-procedures

有人可以告诉我如何为这个mysql语句创建一个存储过程:

SELECT city, country, population
FROM
  (SELECT
     city, country, population, 
     @country_rank := IF(@current_country = country, @country_rank + 1, 1) AS country_rank,
     @current_country := country 
   FROM cities
   ORDER BY country, population DESC
  ) ranked
WHERE country_rank <= 2;

1 个答案:

答案 0 :(得分:1)

至少研究如何使用存储过程

DELIMITER $$

DROP PROCEDURE IF EXISTS `db_name`.`stored_procedure_name`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `stored_procedure_name`(out var1 varchar(100))
BEGIN
    SELECT city, country, population
    FROM
      (SELECT
         city, country, population, 
         @country_rank := IF(@current_country = country, @country_rank + 1, 1) AS country_rank,
         @current_country := country 
       FROM cities
       ORDER BY country, population DESC
      ) ranked
    WHERE country_rank <= 2;

END$$

DELIMITER ;

http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/