tags:

views:

124

answers:

1
SELECT `player`.`cid`, `player`.`k`, `player`.`d`, `gg`.`gg_id`, `gg`.`name`, `gg`.`img`, `cc`.`cid`, `cc`.`name`, `cc`.`class`, `cc`.`gg_id`
FROM `player` 
LEFT JOIN `cc` ON `cc`.`cid` = `player`.`cid` 
LEFT JOIN `gg` ON `gg`.`gg_id` = `cc`.`gg_id` 
ORDER BY (`k`-`d`) DESC

i want to order by the K minus the D values, but im not getting it correctly what im a doing wrong? with or without DESC/ASC, its wrong

+1  A: 

Try:

SELECT (player.k-player.d), player.cid, player.k, player.d, gg.gg_id, gg.name, gg.img, cc.cid, cc.name, cc.class, cc.gg_idFROM player LEFT JOIN cc ON cc.cid = player.cid LEFT JOIN gg ON gg.gg_id = cc.gg_id ORDER BY (player.k-player.d) DESC

I did a quick query of my own and the results appear to be unordered (despite the fact the were) until I added the SELECT (player.k-player.d). MySQL also complained about ommiting the table name in the ORDER BY clause.

Jason George
i get a #1241 - Operand should contain 1 column(s)...if i put table(player.k-player.d), player (k-d) or database(player k - player d) i get unkown colums errors
i did just for a try your simple "select player k - player d from xxx" and i noticed that some of the colums are giving me: 18446744073709551614...they both are INT and have legit values
yeah its definitely something about negative values..i ordered by k+d and it worked..so know what?
ok i changed the colums to FLOAT and know its working...why does mysql needs that, if im not inserting negative numbers there, just getting then in the query? they were INT(11) unsigned, now they are float unsigned
My guess is since the numbers are unsigned MySQL was performing the subtraction as an unsigned operation and the result for negative numbers was an overflow. It's unclear, however, why float works. If you'd like to maintain your column as INT(11) unsigned you can do (CAST(player.k AS SIGNED) - CAST(player.d AS SIGNED)). You could potentailly still get an overflow here, however, if you exceed the maximum value for an unsigned int in either column.
Jason George
hm thanks for your help, im going to make everything "FLOAT" since i only going to put numbers 0-999999 in the columns, i think theres no problem them