Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
MySQL: Want to use a stored procedure to sort table by one column, then use that ordering to set values of another column

Pretty new to SQL, so I apologize if this is obvious.

I have a table of the number of games sold and their corresponding rank in the bestseller list (1, 2, 3, etc.). This table is called ranking(rank: bigint, global_sales: double).

What I want to do is make a stored procedure that I can call whenever the sales update, and then this procedure can be called to update the rankings. Here's what I have so far, and I'm afraid it's probably very incorrect:

delimiter $$
drop procedure if exists updateRank;
create procedure updateRank()
begin
    select *
    from ranking
    order by global_sales desc;
    
    declare r bigint default 1;
    
    loop1: loop
        GameRank = r;
        set r=r+1;
    end loop loop1;
end $$
delimiter ;

From what I could find here and on Google, I couldn't find anything similar, although this is probably a fairly-common query. Any insight would be greatly appreciated.

Edit: I'm using MySQL Workbench version 8.0 CE

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

you can do it in a loop but also you can do it in a query

Of course i don't know your ölayout of your tables, but thos showws you how you would update

CREATE Table ranking (GameRank  INt,global_sales  INT);
INSERT INTO `ranking` VALUES(0,200),(0,300),(0,250),(0,125)
SELECT * FROM ranking
GameRank | global_sales
-------: | -----------:
       0 |          200
       0 |          300
       0 |          250
       0 |          125

MYsql 5.x

SET @ranking = 0
UPDATE ranking r 
INNER JOIN (SELECT @ranking := @ranking + 1 _rank, global_sales FROM ranking ORDER BY global_sales DESC) t  ON  r.global_sales = t.global_sales
  SET GameRank = _rank
SELECT * FROM ranking   OrDER By global_sales DESC
GameRank | global_sales
-------: | -----------:
       1 |          300
       2 |          250
       3 |          200
       4 |          125

MYSQL 8

UPDATE ranking r 
INNER JOIN (SELECT global_sales,RANK() OVER (  ORDER BY global_sales DESC ) my_rank FROM ranking) t  
ON  r.global_sales = t.global_sales
  SET GameRank = my_rank
SELECT * FROM ranking   OrDER By global_sales DESC
GameRank | global_sales
-------: | -----------:
       1 |          300
       2 |          250
       3 |          200
       4 |          125

db<>fiddle here

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda