我有查询,我用来制作收入图表.但现在人们可以从两个不同的来源赚钱,所以我想在同一个图表上将它分成两行
这个标准收益:
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_standard
和earning_referral
.
但是,day
对于x轴,它们都与相同的列/比例对齐- 当特定系列没有收益时,将零替换为0.
您需要将这两个查询都设置为子查询
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.