当前位置:  开发笔记 > 编程语言 > 正文

MySql图查询多个系列对齐到同一时间x轴

如何解决《MySql图查询多个系列对齐到同一时间x轴》经验,为你挑选了1个好方法。

我有查询,我用来制作收入图表.但现在人们可以从两个不同的来源赚钱,所以我想在同一个图表上将它分成两行

这个标准收益:

SELECT DATE_FORMAT(earning_created, '%c/%e/%Y') AS day, SUM(earning_amount) AS earning_standard
FROM earnings
WHERE earning_account_id = ? AND earning_referral_id = 0 AND (earning_created > DATE_SUB(now(), INTERVAL 90 DAY))
GROUP BY DATE(earning_created)
ORDER BY earning_created

这个推荐收益:

SELECT DATE_FORMAT(e.earning_created, '%c/%e/%Y') AS day, SUM(e.earning_amount) AS earning_referral
FROM earnings AS e
INNER JOIN referrals AS r
ON r.referral_id = e.earning_referral_id
WHERE e.earning_account_id = ? AND e.earning_referral_id > 0 AND (e.earning_created > DATE_SUB(now(), INTERVAL 90 DAY)) AND r.referral_type = 0
GROUP BY DATE(e.earning_created)
ORDER BY e.earning_created

如何让它一起运行查询,以便为y轴输出两列/系列:earning_standardearning_referral.

但是,day对于x轴,它们都与相同的列/比例对齐- 当特定系列没有收益时,将零替换为0.



1> wogsland..:

您需要将这两个查询都设置为子查询

SELECT DATE_FORMAT(earnings.earning_created, '%c/%e/%Y') AS day, 
       COALESCE(es.earning_standard, 0) AS earning_standard, 
       COALESCE(er.earning_referral, 0) AS earning_referral
FROM earnings
LEFT JOIN (SELECT DATE_FORMAT(earning_created, '%c/%e/%Y') AS day,   
                  SUM(earning_amount) AS earning_standard
           FROM earnings
           WHERE earning_account_id = ? 
           AND earning_referral_id = 0 
           AND (earning_created > DATE_SUB(now(), INTERVAL 90 DAY))
           GROUP BY DATE(earning_created)) AS es 
ON (day = es.day)
LEFT JOIN (SELECT DATE_FORMAT(e.earning_created, '%c/%e/%Y') AS day,    
                  SUM(e.earning_amount) AS earning_referral
           FROM earnings AS e
           INNER JOIN referrals AS r
           ON r.referral_id = e.earning_referral_id
           WHERE e.earning_account_id = ? 
           AND e.earning_referral_id > 0 
           AND (e.earning_created > DATE_SUB(now(), INTERVAL 90 DAY)) 
           AND r.referral_type = 0
           GROUP BY DATE(e.earning_created)) AS er 
ON (day = er.day)
WHERE earnings.earning_account_id = ?
ORDER BY day

我假设earning_account_id = ?的目的是带有问号,因为您运行查询时使用的语言在运行查询之前将其替换为实际的id.

推荐阅读
放ch养奶牛
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有