0

我正在尝试执行此查询,但出现错误“未定义索引:lname”。我想从表 a 的一列(fname)中计算行并从其他表 b 中选择列(lname)。所以请帮助我。

    $result = mysql_query("SELECT COUNT(fname),lname FROM a,b");
    while ($row = mysql_fetch_array($result))
    {
      echo "<tr><td>";
      echo $row['lname'];
      echo "</td>";
      echo "<td>";
      echo $row['COUNT(fname)'];
      echo "</td></tr>";
    }
4

3 回答 3

3

如果您仍然收到错误,您可以尝试分别获取两者:

$result = mysql_query("SELECT COUNT(fname) FROM a");
while ($row = mysql_fetch_array($result))
  {
    echo "<tr><td>";
    echo $row['COUNT(fname)'];
    echo "</td></tr>";

  }

$result1 = mysql_query("SELECT lname FROM b");
while ($row = mysql_fetch_array($result1))
    {
      echo "<tr><td>";
      echo $row['lname'];
      echo "</td></tr>";
   }
于 2012-10-22T18:55:44.587 回答
2

您需要使用别名。用这个:

$result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
  echo "<tr><td>";
  echo $row['lname'];
  echo "</td>";
  echo "<td>";
  echo $row['countfname'];
  echo "</td></tr>";
}
于 2012-10-18T13:41:59.223 回答
1

试试这个代码:

$result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
  echo "<tr><td>";
  echo $row['lname'];
  echo "</td>";
  echo "<td>";
  echo $row['COUNT(fname)'];
  echo "</td></tr>";
}
于 2012-10-18T13:51:13.907 回答