-3

谁能告诉我如何为这个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;
4

1 回答 1

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/

于 2012-06-29T06:16:09.947 回答