我有两个MYSQL查询,我试图合并为一个,这是第一个 -
SELECT
`Your_Name` as agentname,
sum(`Sale_Amount`) as todaysales,
COUNT(*) as NoSales
FROM mp_wp_sales_tracking
WHERE `created` BETWEEN CURDATE() AND NOW()
GROUP BY agentname
第二个是同一个查询,仅适用于前一天
SELECT
`Your_Name` as agentname,
sum(`Sale_Amount`) as yesterdaysales,
COUNT(*) as YesterdayNoSales
FROM mp_wp_sales_tracking
WHERE `created` BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 DAY)
AND ADDTIME(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'23:59:59.50')
GROUP BY agentname
现在我想做的是,实现类似下面的内容(MYSQL中的别名标题)
| agentname | todaysales | nosales | yesterdaysales | yesterdaynosales |
我已经尝试过使用内部联接,但是一直在尝试使用SQL错误,我不知道在哪里开启它,对于复杂的MYSQL查询来说还是一个新手。
答案 0 :(得分:0)
我可以想到一种方法来合并这两个查询并对结果使用条件聚合
SELECT `Your_Name` as agentname,
SUM( CASE WHEN
`created` BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND ADDTIME(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'23:59:59.50')
THEN `Sale_Amount`
ELSE 0 END
) as yesterdaysales,
SUM(`created` BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND ADDTIME(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'23:59:59.50') ) as YesterdayNoSales ,
SUM(CASE WHEN
`created` BETWEEN CURDATE() AND NOW()
THEN `Sale_Amount`
ELSE 0 END
) as todaysales,
SUM(`created` BETWEEN CURDATE() AND NOW() ) as NoSales
FROM mp_wp_sales_tracking
WHERE `created` BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND ADDTIME(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'23:59:59.50')
OR `created` BETWEEN CURDATE() AND NOW()
GROUP BY agentname
答案 1 :(得分:0)
使用条件聚合(即加法函数中的布尔表达式,例如CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f617eaf4d211 nginx:latest "nginx -g 'daemon ..." About a minute ago Up About a minute (healthy) 80/tcp nginx.1.htgg23ysze00qr0kb790lyxnv
)。
SUM