Jump to content

[SOLVED] I could be crazy...


Aeolus

Recommended Posts

        `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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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`

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.