Hmm, I am not sure about your table structure and what you actuaklly want to do, but assuming that winner is the userid which won the battle and that your user table has fiels battles, wins and losses for the # of battles, wins and losses of each user, the following queries might work:
[sql]
CREATE TEMPORARY TABLE battle_results_temp (userid INT( 10 ) NOT NULL , battles INT( 10 ) NOT NULL , wins INT( 10 ) NOT NULL);
CREATE TEMPORARY TABLE battle_results_temp2 (userid INT( 10 ) NOT NULL , battles INT( 10 ) NOT NULL , wins INT( 10 ) NOT NULL, losses INT (10) NOT NULL);
INSERT INTO battle_results_temp SELECT userid1 AS userid, COUNT(userid1) AS battles, SUM(IF(userid1=winner, 1, 0)) AS wins FROM battle_records GROUP BY userid1;
INSERT INTO battle_results_temp SELECT userid2 AS userid, COUNT(userid2) AS battles, SUM(IF(userid2=winner, 1, 0)) AS wins FROM battle_records GROUP BY userid2;
INSERT INTO battle_results_temp2 SELECT userid, SUM(battles) AS battles, SUM(wins) AS wins, SUM(battles) - SUM(wins) AS losses FROM battle_results_temp GROUP BY userid;
UPDATE battle_results_temp2, user SET user.battles=battle_results_temp2.battles, user.wins=battle_results_temp2.wins, user.losses=battle_results_temp2.losses WHERE battle_results_temp2.userid=user.userid;
[/sql]
|