0

I'm really struggeling with this for some time now. I have a MySQL database and a lot of data. It is a formula1 website i have to create for college.

Right now the j_tracks_rounds_results table is filled with data but one column is not filled out. It's the rank column.

I created a stored procedure as the following:

DROP PROCEDURE `sp_rank`//
delimiter @@
PROCEDURE `sp_rank`(sid INT)
begin
set @i = 0;

SELECT `performance`, subround_id, @i:=@i+1 as rank
FROM `j_tracks_rounds_results` 
where subround_id = sid
order by `subround_id`,`performance`;

end
delimiter ;

The output is like the following:

rec.ID PERFORMANCE     SUBROUND_ID RANK
87766 100,349114516829 1           1
93040 101,075635087628 1           2
88851 101,664302543497 1           3

It gets the results and ads a rank to it, sorted on performance so the lowest performance gets rank1 etc...

What i am trying to achieve is to put the rank back into the table. Like an ALTER command for the column "rank".

How would i be able to accomplish this?

2 Answers 2

0

Basically don't... Create table to hold the key (rec.id ?) and the rank. Truncate it to get rid of the previous results then use insert into ... with your query and then join to it.

You really don't want to be altering tables in your normal running, guaranteed some one will use the column when it isn't there, and then when you look at the fault it will be...

People just don't look for table structures changing through the application lifetime, it's a screw up waiting to happen.

1
  • the population of the new ranking table could be done in a trigger that fires when new rows are inserted into j_tracks_rounds_results
    – ninesided
    Jan 16, 2012 at 23:01
0

You are misapplying your SQL statements. You want the UPDATE command, not ALTER. eg.
UPDATE table SET rank=@i WHERE subround_id=@id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.