SQL计算行号

时间:2015-12-09 09:25:10

标签: php mysql sql

我在做足球联赛,并且有一个球队的简介(比如:Team.php?team = XXX) 在这个页面中,我想展示TeamXXX在联盟表中的位置

页面联盟表

string(19) "2015-12-02 06:36:00"

<?php $number = 0; $sql = "SELECT * FROM `leaguetable` WHERE `league` = 'leaguename' ORDER BY pts DESC"; $query = mysql_query($sql); while($rs=mysql_fetch_assoc($query)){ $number++; ?> <table> <thead> <tr> <th>Position</th> <th>Team</th> <th>Points</th> </tr> </thead> <tbody> <td><?php echo $number; ?></td> <td><?php echo $rs['team']; ?></td> <td><?php echo $rs['pts']; ?></td> </tbody> </table> <?php } ?>

中的数据
leaguetable

在team.php中我想显示TeamXXX的位置

id        team           pts

我怎样才能知道teamXXX在leaguetable中的位置? 任何帮助将不胜感激。非常感谢你

3 个答案:

答案 0 :(得分:0)

<强>查询

    public void addFood(String food, int quantity) {
    if (register.containsKey(food)) {
       Integer newAmount = register.get(food) + quantity;
       register.put(food,newAmount);
      }
   else {
       register.put(food,quantity);
        }
}

<强>使用example.php

select id, team, pts, rnk
from
(
select leag.id, leag.team, leag.pts,
@rnk := if(leag.pts = @lag, @rnk,
           if(@lag := leag.pts, @rnk + 1, @rnk + 1)) as rnk
from leaguetable leag
cross join ( select @rnk := 0, @lag := null ) params
where league = 'FA Cup'
order by leag.pts desc
) rankings
where team = 'Chelsea'
;

<强>输出

<?php

/**
 * Mysqli initial code
 * 
 * User permissions of database
 * Create, Alter and Index table, Create view, and Select, Insert, Update, Delete table data
 * 
 * @package         PhpFiddle
 * @link            http://phpfiddle.org
 * @since           2012
*/

require_once "dBug!.php";

require "util/public_db_info.php";

$short_connect = new mysqli($host_name, $user_name, $pass_word, $database_name, $port);

if (mysqli_connect_errno())
{
  die("Failed to connect to MySQL: " . mysqli_connect_error());
}

/*
$sql = "create table leaguetable"
     . "("
     . "  id integer primary key not null,"
     . "  team varchar(33) not null,"
     . "  pts integer not null default 0"
     . ");";

$result = $short_connect->query($sql);

if(!$result)
{
    die("Create table failed : " . mysqli_error($short_connect));
}

$sql = "insert into leaguetable"
     . "( id, team, pts )"
     . "values"
     . "( 1, 'Liverpool', 22 ),"
     . "( 2, 'Arsenal', 29 ),"
     . "( 3, 'Chelsea', 23 ),"
     . "( 4, 'Tottenham', 23)";

$result = $short_connect->query($sql);

if(!$result)
{
    die("insert failed : " . mysqli_error($short_connect));
}
*/

//get all tables in the database
//$sql = "SHOW TABLES";

//get column information from a table in the database
//$sql="SELECT COLUMN_KEY, COLUMN_NAME, COLUMN_TYPE FROM information_schema.COLUMNS WHERE TABLE_NAME = 'books'";

//SQL statement for a table in the database
$sql = "select id, team, pts, rnk "
     . "from"
     . "("
     . "select leag.id, leag.team, leag.pts,"
     . "@rnk := if(leag.pts = @lag, @rnk,"
     . "           if(@lag := leag.pts, @rnk + 1, @rnk + 1)) as rnk "
     . "from leaguetable leag "
     . "cross join ( select @rnk := 0, @lag := null ) params "
     . "    where league = 'FA Cup' "
     . "order by leag.pts desc;"
     . ") rankings "
     . where team = 'Chelsea';";

//result is boolean for query other than SELECT, SHOW, DESCRIBE and EXPLAIN
$result = $short_connect->query($sql);

if (($result) && ($result->num_rows > 0))
{
    echo "<table>" . "<thead>" . "<tr>" . "<th>Position</th>" . "<th>Team</th>" . "<th>Points</th>" . "</tr>" . "</thead>" . "<tbody>";
    //convert query result into an associative array
        echo "<tr><td>" . $row['rnk'] . "</td><td>" . $row['team'] . "</td><td>" . $row['pts'] . "</td></tr>";
   echo "</tbody></table>";
}
else
{
    die("select failed : " . mysqli_error($short_connect));
}

$short_connect->close();


?>

sqlfiddle

答案 1 :(得分:0)

如果你不介意使用两个查询(我不介意),这很简单:只计算有多少团队有更多积分,并添加一个(如果三个球队并列第二,他们都获得了第二的位置。

$result = mysql_query("SELECT count(*)+1 AS POSTN FROM leaguetable WHERE 
                                league = 'leaguename' AND pts > $points");
$row = mysql_fetch_assoc($result);
$position = $row["POSTN"];

在一个查询中执行此操作有点麻烦,因为您需要将此查询嵌入到原始查询中:

"SELECT *, (SELECT count(*)+1 FROM leaguetable table2 
                WHERE league = 'leaguename' AND table2.pts > leaguetable.pts) AS POSTN 
     FROM leaguetable WHERE team = '$currentteam'"

但为什么要使用mysql_* API来获取新代码?你还注意到文档中的所有dire warnings in pink boxes吗?帮自己一个忙,今天切换到mysqli,从这个程序开始。

此外:从不只是将$_GET[param]注入您的查询字符串!你为自己提供了一个等待发生的SQL注入攻击......以及那时容易出错的错误代码。

答案 2 :(得分:0)

希望这会有所帮助

levels[currentLevel].ColRow;