NULL MAX(col) 值,即使 DB 具有有效记录

NULL MAX(col) value even though DB has a valid record

所以我试图提取当年的最大 invoiceNo,这就是我用 PDO 实现它的方式:

$sql = 'SELECT MAX(invoiceNo) AS invoiceId FROM invoices WHERE invoiceDate BETWEEN :yearStart AND :yearEnd HAVING invoiceId IS NOT NULL';
        if($stmt = $pdo1->prepare($sql)){
            $year = date("Y")."-01-01";
            $stmt->bindParam(":yearStart", $year);
            $year = date("Y")."-12-31";
            $stmt->bindParam(":yearEnd", $year);
            if($stmt->execute()){
                if($stmt->rowCount() == 1){
                    $row = $stmt->fetch(PDO::FETCH_ASSOC);
                    $invoiceNo = $row['invoiceId'];
                    $response = date("Y").strval(++$invoiceNo);
                }
                else{
                    $response = date("Y")."-0";
                }
            }
        }

但是,$response 一直被分配给 else 子句:.

这是我的数据库的样子:

因此我预计 $response 为 2022-2。

我认为我的 SQL 查询有问题,对此我深表歉意,仍在学习中!

$sql = "SELECT MAX(arbitInvoiceNo) AS invoiceId FROM invoices WHERE invoiceDate BETWEEN :dateStart AND :dateEnd HAVING invoiceId IS NOT NULL";
        if($stmt = $pdo1->prepare($sql)){
            $dateStart = date("Y")."-01-01";
            $stmt->bindParam(":dateStart", $dateStart);
            $dateEnd = date("Y")."-12-31";
            $stmt->bindParam(":dateEnd", $dateEnd);
            if($stmt->execute()){
                if($stmt->rowCount() == 1){
                    $row = $stmt->fetch(PDO::FETCH_ASSOC);
                    $invoiceNo = $row['invoiceId'];
                    $response = date("Y")."-".strval(++$invoiceNo);
                }
                else{
                    $response = date("Y")."-0";
                }
            }
        }

更改了我在 PHP 中命名变量的方式和 SQL 查询中的占位符。谢谢 ADyson 的所有帮助!