在运行对所有行进行排名的查询时,从一行中回显一个字段和一个值 [英] Echoing out a field and a value from one row while running a query that ranks all rows

查看:41
本文介绍了在运行对所有行进行排名的查询时,从一行中回显一个字段和一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 $sqlStr5 的 MySQL 查询,它通过名为 totalScore2 的指标对行进行排名.$sqlStr5 返回的字段之一称为 username.

I have a MySQL query called $sqlStr5 that ranks rows by a metric called totalScore2. One of the fields that $sqlStr5 returns is called username.

我想回显 totalScore2 的排名和值,其中 username 等于一个名为 $u 的变量.

I would like to echo out the rank and the value of totalScore2 where username equals a variable called $u.

我该怎么做?

以下是我目前所拥有的.

Below is what I have so far.

提前致谢,

约翰

$result = mysql_query($sqlStr5);
$count = 1;  
$arr = array();

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

            echo '<div class="sitename1edit2a">'.$count++.'.</div>';
            echo '<div class="sitename1edit2">'.number_format(($row["totalScore2"])).'</div>';


        }

推荐答案

这应该有效:

$result = mysql_query($sqlStr5);
$count = 1;

while($row = mysql_fetch_array($result))
{
    if($u == $row['username'])
    {
        echo '<div class="sitename1edit2a">'.$count.'</div>';
        echo '<div class="sitename1edit2">'.number_format($row["totalScore2"]).'</div>';
    }
    $count++;
}

但是请注意,这不是最有效的方法.应该可以进行 SQL 查询,返回用户 $u 的排名和总分.

Note however that this is not the most efficient way. It should be possible to make a SQL query that returns the rank and the total score for user $u.

这篇关于在运行对所有行进行排名的查询时,从一行中回显一个字段和一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆