Skip to content

Instantly share code, notes, and snippets.

@petrovitch
Last active December 21, 2015 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrovitch/910eb1bc34d9d42f5a32 to your computer and use it in GitHub Desktop.
Save petrovitch/910eb1bc34d9d42f5a32 to your computer and use it in GitHub Desktop.
<?php
$query = "
select concat(n.chapter, ': ', lower(n.title)) as ata, coalesce(n.subtotal,0) as in_project, coalesce(n.total,0) as ata_total from (
select
m.chapter, m.title, m.slug,
(select count(*) as subtotal from atas a
inner join parts_listings pl on pl.ata = a.id
inner join project_parts pp on pp.part_listing_id = pl.id
where pl.ata = m.chapter and pp.in_project and a.active and pp.project_id = {$id}
group by a.id order by a.chapter) as subtotal,
(select count(*) as total from atas a
inner join parts_listings pl on pl.ata = a.id
inner join project_parts pp on pp.part_listing_id = pl.id
where pl.ata = m.chapter and a.active and pp.project_id = {$id}
group by a.id order by a.chapter) as total
from
atas m
where
m.active
and
(select count(*) as total from atas a
inner join parts_listings pl on pl.ata = a.id
inner join project_parts pp on pp.part_listing_id = pl.id
where pl.ata = m.chapter and a.active and pp.project_id = {$id}
group by a.id order by a.chapter) > 0
order by
m.chapter
) as n
";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment