请看下面的代码。

SELECT ongoing_portfolio.*,
    Portfolio.Activation,
    SUM(case when Transaction_TimeStamp <= ongoing_portfolio.`Updated_Date`
        then Transactions.`Transaction_Amount` ELSE 0 end) AS `Total`
    FROM Ongoing_Portfolio
    INNER JOIN Portfolio ON Ongoing_Portfolio.idPortfolio = Portfolio.idPortfolio
    INNER JOIN Transactions ON Transactions.`idPortfolio` = Ongoing_Portfolio.idPortfolio
    WHERE ongoing_portfolio.`idPortfolio`= 5
    GROUP BY Ongoing_Portfolio.`Updated_Date`  DESC LIMIT 4


如果特定portfolio至少有1个事务,则“仅”工作正常。如果该投资组合根本没有交易,那么它只会返回空行,这意味着什么也没有。

我尝试与Ongoing_Portfolio.idOngoing_PortfolioTransactions.idTransactionsOngoing_Portfolio.idPortfolio分组,但是一点都不好。为什么会这样发生?

最佳答案

更换

 INNER JOIN Transactions ON Transactions.`idPortfolio` = Ongoing_Portfolio.idPortfolio


通过

 LEFT JOIN Transactions ON Ongoing_Portfolio.idPortfolio = Transactions.`idPortfolio`

关于mysql - Group By返回空行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27563389/

10-12 04:01