Php mysql与日期加入组的总和

时间:2017-02-24 10:39:32

标签: php mysql

您好我需要帮助来解决我的代码。

我尝试了以下查询;

SELECT DATE_FORMAT(buy.date, '%Y-%m-%d') date 
     , SUM(buy.total) sumbuy 
     , SUM(sell.total) sumsell  
  FROM buy  
  JOIN sell  
    ON DATE_FORMAT(buy.date, '%Y-%m-%d') = DATE_FORMAT(sell.date,'%Y-%m-%d') 
 WHERE buy.trans = 'credit' 
   AND sell.trans= 'debit'  
 GROUP 
    BY DATE_FORMAT(buy.date, '%d')

结果是:

   date        sumbuy       sumsell
 ------------------------------------
 2017-02-01    1560000      8080000

我的期望是:

        date        sumbuy       sumsell
     ------------------------------------
     2017-02-01     390000       2020000

这里是完整的表格

购买

total           trans            date
-----------------------------------------------
140000         credit       2017-02-01 04:31:00
50000          credit       2017-02-01 04:32:00
190000         debit        2017-02-01 04:33:00
50000          credit       2017-02-01 04:34:00
150000         credit       2017-02-01 04:35:00

    total         trans         date
   ------------------------------------------
   120000         debit    2017-02-01 04:31:00
   300000         debit    2017-02-01 04:32:00
   800000         debit    2017-02-01 04:33:00
   800000         debit    2017-02-01 04:35:00

请任何人帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

'并且没办法让它变成一张桌子' - 这个断言坦白说是荒谬的。

总之...

DROP TABLE IF EXISTS buy;

CREATE TABLE buy
(total INT NOT NULL
,trans enum('credit','debit')
,date DATETIME NOT NULL);

INSERT INTO buy VALUES
(140000,'credit','2017-02-01 04:31:00'),
( 50000,'credit','2017-02-01 04:32:00'),
(190000,'debit' ,'2017-02-01 04:33:00'),
( 50000,'credit','2017-02-01 04:34:00'),
(150000,'credit','2017-02-01 04:35:00');

DROP TABLE IF EXISTS sell;

CREATE TABLE sell
(total INT NOT NULL
,trans enum('credit','debit')
,date DATETIME NOT NULL);

INSERT INTO sell VALUES
(120000,'debit','2017-02-01 04:31:00'),
(300000,'debit','2017-02-01 04:32:00'),
(800000,'debit','2017-02-01 04:33:00'),
(800000,'debit','2017-02-01 04:35:00');

SELECT DATE(date) date 
     , SUM(CASE WHEN type = 'buy' AND trans = 'credit' THEN total END) sumbuy
     , SUM(CASE WHEN type = 'sell' AND trans = 'debit' THEN total END) sumsell 
  FROM
     ( SELECT * 
            ,'buy' type 
         FROM buy
        UNION 
          ALL
       SELECT *
            ,'sell' 
         FROM sell
     ) x
 GROUP 
    BY DATE(date);
+------------+--------+---------+
| date       | sumbuy | sumsell |
+------------+--------+---------+
| 2017-02-01 | 390000 | 2020000 |
+------------+--------+---------+
1 row in set (0.02 sec)

SQLfiddle

答案 1 :(得分:0)

已经是你问题的解决方案,但也许这个可以在其他情况下帮助其他人

select date, sum(sumbuy) as totalbuy, sum(sumsell) as totalsell from (SELECT DATE_FORMAT(buy.date, '%Y-%m-%d') date 
     , SUM(case when trans = 'credit' then total else 0 end) as sumbuy
     , SUM(case when trans = 'debit' then total else 0 end) as sumsell 
  FROM buy 
  GROUP 
    BY DATE_FORMAT(buy.date, '%d')
union 
SELECT DATE_FORMAT(sell.date, '%Y-%m-%d') date 
     , SUM(case when trans = 'credit' then total else 0 end) as sumbuy
     , SUM(case when trans = 'debit' then total else 0 end) as sumsell 
  FROM sell 
  GROUP 
    BY DATE_FORMAT(sell.date, '%d')) a
    group by date

我注意到你有 2020000 来进行总结,不应该 2210000 ,或者你是否明确地不想要买入表中的借记交易?

如果您只想要销售表中的借方而只需要购买表中的信用额,您可以像这样重写您的查询:

select date, sum(sumbuy) as totalbuy, sum(sumsell) as totalsell from (SELECT DATE_FORMAT(buy.date, '%Y-%m-%d') date 
 , SUM(case when trans = 'credit' then total else 0 end) as sumbuy
 , SUM(case when trans = 'debit' then total else 0 end) as sumsell 
  FROM buy 
 where trans = 'credit'
GROUP 
BY DATE_FORMAT(buy.date, '%d')
union 
SELECT DATE_FORMAT(sell.date, '%Y-%m-%d') date 
 , SUM(case when trans = 'credit' then total else 0 end) as sumbuy
 , SUM(case when trans = 'debit' then total else 0 end) as sumsell 
FROM sell 
where trans = 'debit'
GROUP 
BY DATE_FORMAT(sell.date, '%d')) a
group by date

sqlfiddle