我正在从数据库中提取数据;一些数据没有显示。所有数据都存在
$sql = "SELECT
aid,
sam,
py,
SUM(CASE WHEN MONTH(postDate) = 01 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Jan_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 02 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Feb_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 03 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Mar_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 04 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Apr_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 05 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS May_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 06 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Jun_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 07 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Jul_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 08 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Aug_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 09 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Sep_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 10 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Oct_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 11 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Nov_CreditAmount,
SUM(CASE WHEN MONTH(postDate) = 12 THEN IFNULL(creditAmount, 0) ELSE 0 END) AS Dec_CreditAmount
FROM
t24_trans t
INNER JOIN
tqs_cbnacctnumber c ON t.accountNumber = c.acct_number
WHERE classification = :classification AND YEAR(t.postdate) = 2023
GROUP BY
acct_number,
acct_name,
tqs_tax_type";
$stmt = $conn->prepare($sql);
$classification = 'FEDERATION (FAAC)';
$stmt->bindParam(':classification', $classification);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
echo '<tr>';
echo "<td style='font-size: 14px;'>" . htmlspecialchars($row['tqs_tax_type']) . "</td>";
echo "<td style='font-size: 14px;'>" . htmlspecialchars(number_format(($row['Jan_CreditAmount']) / 1000000, 2)) . "</td>";
echo "<td style='font-size: 14px;'>" . htmlspecialchars(number_format(($row['Feb_CreditAmount']) / 1000000, 2)) . "</td>";
echo "<td style='font-size: 14px;'>" . htmlspecialchars(number_format(($row['Mar_CreditAmount']) / 1000000, 2)) . "</td>";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
我想查看这几个月的所有数据,但有些月份正在显示。我已经尝试了所有方面,但它不起作用我能做什么
在当前查询中,您使用的是 INNER JOIN,它仅返回两个表(t24_trans 和 tqs_cbnacctnumber)中存在匹配的行。这意味着,如果 tqs_cbnacctnumber 表中没有 t24_trans 中给定 accountNumber 的对应记录,则该交易将从结果中排除。这可能是您几个月看不到数据的原因。通过切换到 LEFT JOIN,您可以确保左表 (t24_trans) 中的所有行都包含在结果集中,即使右表 (tqs_cbnacctnumber) 中没有匹配的行。当您想要确保所有交易都包含在报告中,并且 tqs_cbnacctnumber 表中缺少数据只会导致相应列中出现 NULL 值时,这非常有用。