0

私はレンタルと販売の 2 つのテーブルを持っています。ステータス フィールドがあります。ステータス = 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
4

1 に答える 1