Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在从这样的 PDO 查询中获取所有结果:
$orders = $ordersQuery->fetchAll(PDO::FETCH_ASSOC);
现在,我想计算字段“状态”等于“打开”的行数。我知道我可以直接构建我的 SQL 以仅加载 OPEN 状态行,但我认为使用 PHP 和类似的东西可能会更快
$openOrders = count(array_keys($orders,"OPEN"));
它虽然返回 0。
作为记录,这是我在 PHP 中的做法:
$orders = $ordersQuery->fetchAll(PDO::FETCH_ASSOC); $open = array_reduce($orders, function ($num, array $order) { return $num + (int)($order['status'] == 'OPEN'); }, 0);
以下是实际操作方法:
$query = "SELECT COUNT(*) FROM orders WHERE status = 'OPEN'";