我有两个表出租和销售。有一个字段状态。如果状态 = 1 是未发布,状态 = 2 是发布,状态 = 3 是待定。
我想找到代理的租金和销售中所有发布、取消发布和待处理的总和。
这是我尝试过的,但它给了我错误的数据
select sum(publish1) publish, sum(unpublish1) unpublish, sum(pending1) pending, agent_id,status from (
select agent_id,status, count(*) as publish1, 0 unpublish1, 0 pending1 from rentals where status = 2 GROUP BY agent_id
union all select agent_id,status, 0 publish1, count(*) as unpublish1, 0 pending1 from rentals where status = 1 GROUP BY agent_id
union all select agent_id,status, 0 publish1, 0 pending1, count(*) as pending1 from rentals where status = 3 GROUP BY agent_id
union all select agent_id,status, count(*) as publish1, 0 unpublish1, 0 pending1 from sales where status = 2 GROUP BY agent_id
union all select agent_id,status, 0 publish1, count(*) as unpublish1, 0 pending1 from sales where status = 1 GROUP BY agent_id
union all select agent_id,status, 0 publish1, 0 pending1, count(*) as pending1 from sales where status = 3 GROUP BY agent_id ) s GROUP BY agent_id