我陷入了计数问题。任何人都可以查看我的下面的代码并告诉我如何从第一个while循环计数,这是$ query。这里有两列分组,我只想添加另一个列名而不分组只计算行数。像
"SELECT CountryCode,NetID,COUNT(OpCode) as count FROM activecon WHERE OpCode='$thisActiveOp' GROUP BY CountryCode,NetID"
我尝试了以上但我不知道如何使用我的底部代码回显此COUNT(OpCode),我用$ countryValue和$ networkValue回显我的表行。
$query=mysql_query("SELECT CountryCode,NetID FROM activecon WHERE OpCode='$thisActiveOp' GROUP BY CountryCode,NetID") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
echo "<tr><td>$thisActiveOp</td>";
$networkID = $row['NetID'];
$countryID = $row['CountryCode'];
$query2=mysql_query("SELECT countryName,networkName FROM connection_details WHERE countryId='$countryID' AND netId='$networkID'") or die(mysql_error());
while($row2 = mysql_fetch_array($query2)) {
$countryValue = $row2['countryName'];
$networkValue = $row2['networkName'];
}
echo "<td>$countryValue</td><td>$networkValue</td></tr>";
}
答案 0 :(得分:1)
我认为您建议的代码应该有效,所以它将是:
$query=mysql_query("SELECT CountryCode,NetID,COUNT(OpCode) as code FROM activecon WHERE OpCode='$thisActiveOp' GROUP BY CountryCode,NetID") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
echo "<tr><td>$thisActiveOp</td>";
$networkID = $row['NetID'];
$countryID = $row['CountryCode'];
$code = $row['code'];
$query2=mysql_query("SELECT countryName,networkName FROM connection_details WHERE countryId='$countryID' AND netId='$networkID'") or die(mysql_error());
while($row2 = mysql_fetch_array($query2)) {
$countryValue = $row2['countryName'];
$networkValue = $row2['networkName'];
}
echo "<td>$countryValue</td><td>$networkValue</td><td>$code</td></tr>";
}
答案 1 :(得分:0)
您可以使用别名来从mysql返回的数组中访问计数。
但是根据数据的详细信息,您的脚本/ sql可能会简化为此(即假设activecon上的每条记录在connection_details上只有1条记录): -
<?php
$query=mysql_query("SELECT b.countryName, b.networkName, COUNT(a.OpCode) AS `count`
FROM activecon a
INNER JOIN connection_details b
ON a.CountryCode = b.countryId AND a.NetID = b.netId
WHERE a.OpCode='".mysq_real_escape_string($thisActiveOp)."'
GROUP BY b.countryName, b.networkName") or die(mysql_error());
while($row = mysql_fetch_array($query))
{
echo "<tr><td>$thisActiveOp</td><td>".$row['countryName']."</td><td>".$row['networkName']."</td><td>".$row['count']."</td></tr>";
}