是否可以避免循环查询?

时间:2018-02-15 14:56:27

标签: php mysql query-performance

我有这样的代码:

echo "Choose the polling interval: "
read interval
while :
do
echo "starting tail"
tail -f -n0 /var/log/messages &
pid=$!
echo "pid is $pid"

echo "checking ps for tail"
ps -ef | grep $pid
echo "checked ps for tail"

(sleep "$interval" ; kill "$pid") &
killpid=$!

echo "counting $interval seconds"
sleep $interval

echo ""
echo "killpid is $killpid"

wait $killpid


echo ""
echo "$interval seconds have passed"

echo "killpid $killpid should be dead now"

echo ""

echo "checking ps for kill"
ps -ef | grep $killpid
echo "checked ps for kill"

echo ""

echo "tail pid $pid should be dead now"

echo "rechecking ps for tail"
ps -ef | grep $pid
echo "rechecked ps for tail"

echo ""


done

它从数据库中读取日期并打印带有数据的单独表。没有循环查询就可以实现这个目的吗?

1 个答案:

答案 0 :(得分:3)

您可以按日期而不是分组依据来排序结果。然后,您可以使用一些if条件来呈现h1table标记:

$q = "SELECT `smth`, `date` FROM `t1` ORDER BY `date`";
$pdo->query($q);
$results = $pdo->results();

$currentDate = '';
foreach($results as $index => $result) {
    if ($currentDate != $result['date']) {
        if ($index != 0) {
            echo "</table>";
        }

        echo "<h1>{$result['date']}</h1>";
        echo "<table>";
    }

    echo "<tr><td>{$result['smth']}</td></tr>";

    $currentDate = $result['date'];
}

echo "</table>";

注意: 未经过测试