Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- part_id | part_name
- post_id | part_id | type | title | content
- part_id | part_name | count_posts_type1 | count_posts_type2 | count_posts_type3
- select
- pa.part_id, pa.part_name,
- coalesce(sum(case when po.type = 1 then 1 else 0 end), 0) as count_posts_type1,
- coalesce(sum(case when po.type = 2 then 1 else 0 end), 0) as count_posts_type2,
- coalesce(sum(case when po.type = 3 then 1 else 0 end), 0) as count_posts_type3
- from parts pa
- left join posts po on po.part_id = pa.part_id
- group by pa.part_id, pa.part_name
- SELECT
- parts.part_id,
- parts.part_name,
- COUNT(t1.post_id) AS count_posts_type1,
- COUNT(t2.post_id) AS count_posts_type2,
- COUNT(t3.post_id) AS count_posts_type3
- FROM posts
- RIGHT JOIN part ON posts.part_id=parts.part_id
- INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t1(post_id) ON posts.post_id = t1.post_id
- INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t2(post_id) ON posts.post_id = t2.post_id
- INNER JOIN ( SELECT post_id FROM posts WHERE type=1 ) as t3(post_id) ON posts.post_id = t3.post_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement