【发布时间】:2011-10-14 15:40:39
【问题描述】:
代码 sn-p 旨在返回此表中每一轮的最小和最大 id 我只是想知道是否有一种不那么笨重的方法。
$query = "SELECT round FROM $tablename";
$rounds = Array();
if ($result = $Login->mysqli->query($query)) {
while ($row = $result->fetch_array(MYSQL_ASSOC)) {
$rounds[] = $row['round'];
}
}
$rounds = array_unique($rounds);
$rounds = array_values($rounds);
$roundBound = Array();
foreach ($rounds as $roundNum) {
$query = "SELECT MIN(id), MAX(id) FROM $tablename WHERE round = $roundNum;";
$result = $Login->mysqli->query($query);
$row = $result->fetch_row();
$roundBound[] = $row[0];
$roundBound[] = $row[1];
}
快点……
改为,
$query = "SELECT MIN(id), MAX(id) FROM $tablename GROUP BY round";
if($result = $Login->mysqli->query($query)) {
while ($row = $result->fetch_row()) {
$roundBound[] = $row[0];
$roundBound[] = $row[1];
}
}
【问题讨论】:
-
我想我不需要 array_values 调用,因为我正在通过 foreach 进行调用...
-
这是萨吉和马克之间的折腾。谢谢你们俩。
标签: php optimization mysqli