我将如何按类型过滤?和另一个左连接正确吗?

时间:2015-03-25 14:05:47

标签: mysql left-join

我想根据交易存款类型=>进行过滤transactions.type以及如何在此查询中加入另一个表?

SELECT 
    calendar.datefield AS DATE,
    Ifnull(Sum(transactions.deposit_amount), 0) AS total_deposits,
    transactions.type
FROM transactions
RIGHT JOIN calendar
ON (Date(transactions.created_at) = calendar.datefield)
WHERE      
(
  calendar.datefield BETWEEN
        (
            SELECT Min(Date(created_at))
            FROM   transactions
        )
    AND
        (
             SELECT Max(Date(created_at))
             FROM   transactions
         )
 ) 

1 个答案:

答案 0 :(得分:0)

    SELECT calendar.datefield AS DATE,
   Ifnull(Sum(transactions.deposit_amount),
   0) AS total_deposits,
   type  
FROM
   transactions         
RIGHT JOIN
   calendar                 
      ON (
         Date(transactions.created_at) = calendar.datefield 
      )  
WHERE
   (
      calendar.datefield BETWEEN (SELECT
         Min(Date(created_at))                                       
      FROM
         transactions) AND                                             (
         SELECT
            Max(Date(created_at                                                         ))                                              
         FROM
            transactions
      ) 
   )         
   AND type = 'Deposit'  
GROUP  BY
   date