Php mysql和按日期加入组
你好,我需要帮助来解决我的代码在这里 我尝试了以下查询Php mysql和按日期加入组,php,mysql,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
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
这是完整的桌子
买
出售
请任何人帮我解决这个问题。“没有办法把它变成一张桌子”——坦率地说,这个断言是荒谬的 无论如何
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)
您的问题已经有了解决方案,但也许这一方案可以在其他情况下帮助其他人
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
我注意到您在sumsell中获得了2020000,不应该是221000,或者您明确不希望从buy表中获得借记交易
如果您只需要sell表中的借项,而只需要buy表中的贷项,您可以像这样重写查询:
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
对于我来说,有一个买入表和卖出表,以及一个可以是借方和贷方的trans列,这似乎让我感到困惑。看,我很困惑。为什么不只有一个表呢?我的项目需要两个表,这是一个例子,所以我很简单,:dw为什么需要两个表?你可以按类型推迟交易。因为两个表都有借记和贷记交易,无法成为一个表140000+50000+50000+150000=390000和@草莓再次击败我,仍然在努力寻找更好的解决方案,也许我需要将我的尼克改为?嗯,非常有用的答案。非常感谢你,草莓。@草莓,在这种情况下只有几条记录,但是在许多记录的情况下,哪种解决方案工作得更快?@davejal我怀疑其中没有太多内容,但如果不进行测试,我就不知道了。
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
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