Question

I'm having trouble getting the ranked value for a variable and updating it to a column. Basically what I mean is that below I have a code that selects column "bankaccount" from table "bank" and i'm ranking the bank accounts based on the bank balance. How would I be able to select to update the column "bankaccountranking" based on what the rating each account got.

Example: Ranked 1 bank account is John Smith. How would I write a code so that I UPDATE John Smith's "bankaccount ranking" column in table "bank"? I tried to update using the $rank variable but that doesnt work.

Below is the code: (thanks in advance):

$sessionid = $_SESSION['uid'];
$info = $conn->prepare("SELECT `bankaccount`,`bankbalance` FROM `bank` WHERE           id:id      ORDER BY `bankbalance` DESC");
$info ->bindParam(':id', $sessionid , PDO::PARAM_INT);
$info->execute();


 $rank = 0;


while ($userinfo = $info->fetchobject()) {
$rank++;
echo "<b>$rank</b> . $userinfo->bankaccount &nbsp&nbsp&nbsp <b>Bank                    Balance:</b>           $userinfo->bankbalance </br>"; 

}

Was it helpful?

Solution

Instead of constantly hitting the database with multiple queries, consider to do it at once like this

UPDATE bank t JOIN 
(
  SELECT id, bankaccount, 
  (
    SELECT COUNT(*)
      FROM bank
     WHERE id = b.id
       AND bankbalance > b.bankbalance
  ) + 1 rank
    FROM bank b
   WHERE id = 1
) s 
   ON t.id = s.id
  AND t.bankaccount = s.bankaccount
  SET t.bankaccountranking = rank;

Here is SQLFiddle demo

or with two statements, leveraging user variables and ORDER BY in UPDATE

SET @rnum = 0;
UPDATE bank
   SET bankaccountranking = (@rnum := @rnum + 1)
 WHERE id = 1
 ORDER BY bankbalance DESC;

Here is SQLFiddle demo


Now php code might look like this

$sessionid = $_SESSION['uid'];

$sql = "UPDATE bank t JOIN 
(
  SELECT id, bankaccount, 
  (
    SELECT COUNT(*)
      FROM bank
     WHERE id = b.id
       AND bankbalance > b.bankbalance
  ) + 1 rank
    FROM bank b
   WHERE id = :id
) s 
   ON t.id = s.id
  AND t.bankaccount = s.bankaccount
  SET t.bankaccountranking = rank;";

$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $sessionid , PDO::PARAM_INT);
$stmt->execute();

UPDATE: to implement equivalent of DENSE_RANK() analytic function with a subquery you can do

UPDATE bank t JOIN 
(
  SELECT id, bankaccount, 
  (
    SELECT COUNT(DISTINCT bankbalance)
      FROM bank
     WHERE id = b.id
       AND bankbalance > b.bankbalance
  ) + 1 rank
    FROM bank b
   WHERE id = 1
) s 
   ON t.id = s.id
  AND t.bankaccount = s.bankaccount
  SET t.bankaccountranking = rank;

Here is SQLFiddle demo

or with user(session) variables

SET @r = 0, @b = NULL; 
UPDATE bank b JOIN
(
  SELECT id, bankaccount, @r := IF(@b = bankbalance, @r, @r + 1) rank, @b := bankbalance
    FROM bank
   WHERE id = 1
   ORDER BY bankbalance DESC
) s
    ON b.id = s.id
   AND b.bankaccount = s.bankaccount
   SET bankaccountranking = rank;

Here is SQLFiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top