Aeolus Posted August 19, 2009 Share Posted August 19, 2009 `nat_d` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin})) AS `d_score`, `nat_c` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin})) AS `c_score`, `nat_j` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin})) AS `j_score`, `d_score` + `c_score` + `j_score` AS `score` this is what I'm trying to do... but it's not working.. :/ The first three rows work fine but the last doesn't... Quote Link to comment Share on other sites More sharing options...
Aeolus Posted August 19, 2009 Author Share Posted August 19, 2009 Any help? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 19, 2009 Share Posted August 19, 2009 Hello again. I think you meant columns where you said rows, so you might be crazy after all, sorry. MySQL doesn't necessarily work like a programming language. From programming experience, our intuition would tell us d_score is defined first, then c_score, then j_score, so we should be able to use them as identifiers. But that's not the case. There are two ways you can probably work around this. The first is with sub-queries. SELECT a.*, a.`d_score` + a.`c_score` + a.`j_score` as `score` FROM ( SELECT ... `nat_d` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin})) AS `d_score`, `nat_c` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin})) AS `c_score`, `nat_j` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin})) AS `j_score` ... FROM ... REST_OF_YOUR_QUERY ) AS `a` ORDER BY ... What happens here is the sub-query is run first. The results (or records) of the sub-query are then SELECTed from by the outer query just as if they were an actual table in the database (and we gave that "table" a temporary name of `a`). Essentially we are performing the calculations and temporarily saving them so that we can select them again AND use them to perform more calculations. The other way involves MySQL variables like the other queries I helped you with. You'll need to make two calls to mysql_query(). #1 SET @DVAR=0, @CVAR=0, @JVAR=0; #2 @DVAR:=(`nat_d` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin}))) AS `d_score`, @CVAR:=(`nat_c` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin}))) AS `c_score`, @JVAR:=(`nat_j` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin}))) AS `j_score`, @DVAR + @CVAR + @JVAR AS `score` This creates some variables in MySQL (just like PHP variables) that you can use in your queries. So each of those column values is computed and stored in a variable, and then all three variables are used to compute the last column. I'm not 100% sure this will work, but it might. Quote Link to comment Share on other sites More sharing options...
Aeolus Posted August 19, 2009 Author Share Posted August 19, 2009 This part worked @DVAR:=(`nat_d` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin}))) AS `d_score`, @CVAR:=(`nat_c` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin}))) AS `c_score`, @JVAR:=(`nat_j` + FLOOR( {$natModifierMin} + RAND() * ({$natModifierMax} - {$natModifierMin}))) AS `j_score`, @DVAR + @CVAR + @JVAR AS `score` Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.