Jump to content

Virtual columns


takky

Recommended Posts

Hi

 

Just trying to save on some php by trying this query:

 

  $query = "SELECT @rank := @rank +1 AS rank, userid, percent FROM ranking ORDER BY percent ASC";

 

Which I have tried directly against the mysql instance with total success.  Trying from within PHP the rank column doesn't seem to be there...  Is this even allowed, do the @ need escaping, any suggestions welcome... even if I have to use a view, please let me know.

 

Heres the code fragment:

 

    $conn = get_db_conn();

    $query = "SELECT @rank := @rank +1 AS rank, userid, percent FROM ranking ORDER BY percent ASC";

    $res = mysql_query($query,$conn);

    while($row = mysql_fetch_array($res)){

        echo $row['rank'], " ";

        echo $row['userid'], " ";

        echo $row['percent'], " <br>";

    }

 

The userid and percent are echoed the rank is simply missing... 

 

Thanks for any hep.

 

PHP newbie :(

Link to comment
Share on other sites

that's helpful for me as a newbie, thank you.  I also checked the SQL itself Here's the some resultset:

 

SELECT @rank := @rank +1 AS rank, userid, percent FROM ranking ORDER BY percent DESC

Array

(

    [0] =>

    [rank] =>

    [1] => 573522302

    [userid] => 573522302

    [2] => 11.36

    [percent] => 11.36

)

 

As you can see the rank "virtual column" contains no value...  Do I conclude such virtual columns as unsupported?

Link to comment
Share on other sites

i remember now... that bit of the query wasn't working so i took it off and left just the it from the SELECT.  Seems I have to set the rank firstly on this connection and then select the rank

 

    $query = "SET @rank := 0";
    mysql_query($query,$conn);
    $query = "SELECT @rank := @rank +1 AS rank, userid, percent FROM ranking ORDER BY percent DESC";
    echo "<pre>",$query,"</pre>";

 

Could these I combine these into one mysql_query() call, seemingly it doesn't like me using a ; to concatenate these statements?

 

... but i'm helped and many thanks

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.