带有LIMIT别名的SQL SUM返回"未定义的索引"

时间:2014-07-30 09:11:18

标签: php mysql subquery

有人能告诉我这段代码有什么问题吗?该页面返回:

Notice: Undefined index: totlvlown in /home/admin/domains/reincarnationgame.com/public_html/alliancespar.php on line 82

我的代码是:

$query = $dbcon->prepare('SELECT sum(level) FROM (SELECT player.level FROM player WHERE player.alliance = '.$alliance.' ORDER BY player.level DESC LIMIT '.$playersown.') AS totlvlown');
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$totallvlown = $row['totlvlown'];

变量$ alliance和$ playersown是简单的整数。稍后将替换为绑定值。

我是子查询的新手,似乎无法使其发挥作用。我尝试将AS部分移动到FROM部分之前,但这只是告诉我所有表都需要别名。

1 个答案:

答案 0 :(得分:0)

尝试此解决方案因为我认为您必须先创建表名alias name,以便alias

能够识别级别sum function
$queryalliancelvlown = $dbcon->prepare('SELECT player level sum(level)
FROM 
(
  SELECT player.level
  FROM player
  WHERE player.alliance = '.$alliance.' 
  ORDER BY player.level DESC LIMIT '.$playersown.'
) AS totlvlown');