sql >> Database teknologi >  >> RDS >> Mysql

MySQL lagrede procedurevariabler fra SELECT-sætninger

Rettede et par ting og tilføjede et alternativt valg - slet efter behov.

DELIMITER |

CREATE PROCEDURE getNearestCities
(
IN p_cityID INT -- should this be int unsigned ?
)
BEGIN

DECLARE cityLat FLOAT; -- should these be decimals ?
DECLARE cityLng FLOAT;

    -- method 1
    SELECT lat,lng into cityLat, cityLng FROM cities WHERE cities.cityID = p_cityID;

    SELECT 
     b.*, 
     HAVERSINE(cityLat,cityLng, b.lat, b.lng) AS dist 
    FROM 
     cities b 
    ORDER BY 
     dist 
    LIMIT 10;

    -- method 2
    SELECT   
      b.*, 
      HAVERSINE(a.lat, a.lng, b.lat, b.lng) AS dist
    FROM     
      cities AS a
    JOIN cities AS b on a.cityID = p_cityID
    ORDER BY 
      dist
    LIMIT 10;

END |

delimiter ;


  1. Opret en ny Ruby on Rails-applikation ved hjælp af MySQL i stedet for SQLite

  2. Aktiver fjernadgang til MySQL-database

  3. Får alle brugere undtagen administratorer i mange-til-mange forhold

  4. PDO tilbudsmetode