I’m glad you worked that out, even when I was working with MySQl directly on a regular bases I would not have been able to right that query, or at least not without it taking me much longer than it did you.
This will probably be a good example for others.