Goal:
We want to display a list of Newsletter Topics, and show how many active subscribers there are for each topic.
Table Structure:
You have 3 tables.
Topics – a list of topics for newsletters
Subscribers – a list of subscribers to newsletters
Subscribers_Topics - a table to join subscribers to Topics
Explanation:
An INNER JOIN can only join 2 tables together.
We need to join 3 tables together. So we must “nest†the first join between 2 tables inside the first inner join.
INNER JOIN (
Subscribers as NS
INNER JOIN
Subscribers_Topics AS NST
ON NS.ID=ST.Subscriber_ID
)
ON
NT.ID=NST.Topic_ID
We use GROUP BY to combine rows that have the same field values, so we can get a count of them.
We use HAVING as a filter to limit the records that are returned. This is the same theory as using the WHERE operator, but since we are using GROUP BY, we need to use the HAVING function to limit the results.
Added by JC on 14th November, 2007
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment