"get a list of categories, with the number of tutorials in them, ..."
Code:
select categories.name as catname
, categories.id as catid
, count(tid) as numtut
from categories
left outer
join tutorial_lookup
on categories.id
= tutorial_lookup.cid
group
by categories.name
, categories.id
"... and also list tutorials under them"
Code:
select categories.name as catname
, categories.id as catid
, tutorials.name as tutname
, tutorials.id as tutid
from categories
left outer
join tutorial_lookup
on categories.id
= tutorial_lookup.cid
left outer
join tutorials
on tutorial_lookup.tid
= tutorials.id
now, it appears as though you wanted to combine these results
that's possible, with UNION
Code:
select categories.name as catname
, categories.id as catid
, 1 as sortseq
, count(tid) as numtut
, cast(null as varchar) as tutname
, cast(null as integer) as tutid
from categories
left outer
join tutorial_lookup
on categories.id
= tutorial_lookup.cid
group
by categories.name
, categories.id
union all
select categories.name
, categories.id
, 2
, cast(null as tinyint)
, tutorials.name
, tutorials.id
from categories
left outer
join tutorial_lookup
on categories.id
= tutorial_lookup.cid
left outer
join tutorials
on tutorial_lookup.tid
= tutorials.id
order
by catname
, catid
, sortseq
, tutname
, tutid
Bookmarks