我有两个不同的表,领先和点击。我想查询MySQL以获得按日期排序的点击和潜在客户产生的佣金总和。
铅:
id|date |commission
1|2009-06-01|400
2|2009-06-01|300
3|2009-06-03|350
点击:
id|date |commission
1|2009-06-01|1
2|2009-06-03|2
3|2009-06-03|1
我想创建一个查询给我(如果可能的话,也会获得没有生成潜在客户或点击的日期):
date |commission click|commission lead|total commission
2009-06-01| 1| 700| 701
2009-06-02| 0| 0| 0
2009-06-02| 3| 350| 353
(日期实际上是真实数据库中的日期时间。)
我想我必须结合起来:
SELECT count(*) as number_clicks, sum(click.commission) as sum_clicks,
date(click.time) as click_date from click group by click_date order by click_date
使用:
SELECT count(*)as number_leads, sum(lead.commission) as sum_leads,
date(lead.time) as lead_date from lead group by lead_date order by lead_date
但我不能让他们一起工作。
答案 0 :(得分:2)
这不会使用零获取日期,因为您需要日期表或存储过程来循环日期。一种方法是从联合查询(未经测试)中进行子选择:
SELECT commission_date, SUM(click_commission), SUM(lead_commission), SUM(total_commission)
FROM (SELECT DATE(click.time) AS commission_date, click.commission AS click_commission,
0 AS lead_commission, click.commission AS total_commission
FROM click
UNION ALL
SELECT DATE(lead.time) AS commission_date, 0 AS click_commission,
lead.commission AS lead_commission, lead.commission AS total_commission
FROM lead) AS foo
GROUP BY commission_date
ORDER BY commission_date