I started with a query:
SELECT strip.name as strip, character.name as character from strips, characters, appearances where strips.id = appearances.strip_id and characters.id = appearances.character.id and appearances.date in (...)
Which yielded me some results:
strip | character 'Calvin & Hobbes' | 'Calvin' 'Calvin & Hobbes' | 'Hobbes' 'Pearls Before Swine' | 'Pig' 'Pearls Before Swine' | 'Rat' 'Pearls Before Swine' | 'Hobbes' # a guest appearance 'Pearls Before Swine' | 'Calvin' # a guest appearance
Then I wanted to also get the COUNT of the number of times a character is used (in any strip) within the result set. So I tried:
SELECT count(character.id), strip.name as strip, character.name as character from strips, characters, appearances where strips.id = appearances.strip_id and characters.id = appearances.character.id and appearances.date in (...)
But that gave me
[ERROR 11:20:17] Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
So I tried:
SELECT count(character.id), strip.name as strip, character.name as character from strips, characters, appearances where strips.id = appearances.strip_id and characters.id = appearances.character.id and appearances.date in (...) group by character.id
Which gave me
count | strip | character 4 | 'Calvin & Hobbes' | 'Calvin' 4 | 'Calvin & Hobbes' | 'Hobbes' 2 | 'Pearls Before Swine' | 'Pig' 2 | 'Pearls Before Swine' | 'Rat'
That is, I lose all the extra information about exactly which characters appear in which strips.
What I’d like to get is this:
count | strip | character 4 | 'Calvin & Hobbes' | 'Calvin' 4 | 'Calvin & Hobbes' | 'Hobbes' 2 | 'Pearls Before Swine' | 'Pig' 2 | 'Pearls Before Swine' | 'Rat' 4 | 'Pearls Before Swine' | 'Calvin' 4 | 'Pearls Before Swine' | 'Hobbes'
But I can’t seem to figure it out. I’m on MySQL if it matters. Perhaps it’ll just take two queries.
Does mySQL support analytic functions? Like:
Alternatively: