以下是在Php中执行的两个MySql查询。
乍一看,他们实现了我想要的目标。它将计算团队中有多少用户,然后计算Locations出现的频率,然后对它们进行分组,返回最高数量,以便将其用作变量$ flagcount。
但是,如果没有用户,因此没有位置,它将返回上一个循环的结果。当我在PhpMyAdmin中运行查询时,它不返回结果/空集。
我已经尝试过NULLIF(你可以看到,并且你可以看到位置!=''。无论出于何种原因,如果它为空/空,它将使用先前的循环位置计数作为结果。
/** other table select --working loop-- */
/** irrelevant code here */
foreach( $rows as $result ) {
$result = $conn->prepare("SELECT location, COUNT(NULLIF(TRIM(location), '')) as flagnum FROM users WHERE team_name='$teamname' AND location !='' GROUP BY location ORDER BY flagnum DESC LIMIT 1");
$result->execute();
$rows = $result->fetchAll();
foreach( $rows as $result ):
$flagmajority = $result['location'];
$flagcount = $result['flagnum'];
endforeach;
$result = $conn->prepare("SELECT team_name, COUNT(NULLIF(TRIM(team_name), '')) as playnum FROM users WHERE team_name='$teamname' AND team_name !='' GROUP BY team_name ORDER BY playnum DESC LIMIT 1");
$result->execute();
$rows = $result->fetchAll();
foreach( $rows as $result ):
$playercount = $result['playnum'];
endforeach;
$flagpercent = ($flagcount/$playercount) * 100;
$roundedpercent = round($flagpercent);
echo $flagcount;
}
答案 0 :(得分:0)
尝试在循环中初始化$ flagcount。
像这样:
(.+(\W(data((-)|(\s))driven)\W).+)