2014-12-19 108 views
0

請看下面的代碼。`Group By`返回空行

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 

這工作正常「只」,如果有「至少」1特定portfolio交易。如果該組合沒有交易,那麼它只是返回空行,這意味着什麼都沒有。

我試着與Ongoing_Portfolio.idOngoing_Portfolio,Transactions.idTransactions,Ongoing_Portfolio.idPortfolio分組,但沒有任何好處。爲什麼會發生這種情況?

+1

使用替換

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

一個'左join',而不是'內join' – 2014-12-19 09:50:54

+0

@juergend:OK,但什麼? – 2014-12-19 09:51:56

回答

1

通過

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

嘗試使用LEFT JOIN而不是INNER JOIN的