-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 に答える