My name is Cédric Bozzi, I make apps and websites, and this is my tech blog — you’ll find news commentary here, from a very opinionated Mac-head.
Il y a une version française ici, but most of this blog’s contents are extracted from my Twitter feed, and hence only available in one language (which varies randomly).
Okay, I fled SQL courses like the plague in school (if you had told me then I’d be spending all of my time writing MySQL queries… well, let’s say I wouldn’t have lived to taste Coke Zero) but I think I would still be choking on this one if I hadn’t.
So, let’s say I’ve got a table of comments; each comment is associated with a post ID, obviously. If I want to know how many comments there are per post, I can write:
SELECT POST_ID, COUNT(*) FROM COMMENTS GROUP BY POST_ID
But what if I want to know that count plus some contents from the last comment on each post? Is there a way to do that without making one query per post?
(Actually, even if I didn’t want to get COUNT(*), I still wouldn’t know how to do that in just one query.)
2001 01 02 03 04 05 06 07 08 09 10 11 12
2002 01 02 03 04 05 06 07 08 09 10 11 12
2003 01 02 03 04 05 06 07 08 09 10 11 12
2004 01 02 03 04 05 06 07 08 09 10 11 12
2005 01 02 03 04 05 06 07 08 09 10 11 12
2006 01 02 03 04 05 06 07 08 09 10 11 12
2007 01 02 03 04 05 06 07 08 09 10 11 12
2008 01 02 03 04 05 06 07 08 09 10 11 12
2009 01 02 03 04 05 06 07 08 09 10 11 12
2010 01 02 03 04 05 06 07 08 09 10 11 12