在查询中使用右连接有一个奇怪的问题

Having a strange issue using right join in query

我有这个 MySQL 查询,它应该提供过去 30 天数据库中所有银行账户的进出金额。它甚至通过使用带有日历 table.

的连接语句来填补空白

然而,我 运行 遇到了限制数据集仅包含属于特定帐户的报表的问题,该帐户由 account_id 字段定义。

这是为特定帐户获取数据库中所有条目的原始查询:

SELECT
    COUNT(transactions.account_id) AS numTransactions,
    calendar.datefield AS date,
    SUM(CASE WHEN transactions.amount < 0 THEN transactions.amount ELSE 0 END) as negativeAmount,
    SUM(CASE WHEN transactions.amount > 0 THEN transactions.amount ELSE 0 END) as positiveAmount
FROM
    transactions
RIGHT JOIN
    calendar
ON
    DATE(transactions.accounting_date) = calendar.datefield
WHERE
    calendar.datefield BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW()
AND
    transactions.account_id = 1
GROUP BY
    date
ORDER BY
    date DESC;

这将 return 仅具有 transactions.amount 值的日期忽略 RIGHT JOIN calendar 子句。

我在这里错过了什么?

我其实很快就解决了。

我的 WHERE 子句在左边 table,而不是右边 (?)。

这有效:

SELECT
    COUNT(transactions.account_id) AS numTransactions,
    calendar.datefield AS date,
    SUM(CASE WHEN transactions.amount < 0 THEN transactions.amount ELSE 0 END) as negativeAmount,
    SUM(CASE WHEN transactions.amount > 0 THEN transactions.amount ELSE 0 END) as positiveAmount
FROM
    transactions
RIGHT JOIN
    calendar
ON
    DATE(transactions.accounting_date) = calendar.datefield
AND
    transactions.account_id = 1
WHERE
    calendar.datefield BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW()
GROUP BY
    date
ORDER BY
    date DESC;