FREN

#FF00AA


17 oct. 2007

Lazyweb, MySQL edition

@web@

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.)

Want to know when I post new content to my blog? It's a simple as registering for free to an RSS aggregator (Feedly, NewsBlur, Inoreader, …) and adding www.ff00aa.com to your feeds (or www.garoo.net if you want to subscribe to all my topics). We don't need newsletters, and we don't need Twitter; RSS still exists.

Legal information: This blog is hosted par OVH, 2 rue Kellermann, 59100 Roubaix, France, www.ovhcloud.com.

Personal data about this blog's readers are not used nor transmitted to third-parties. Comment authors can request their deletion by e-mail.

All contents © the author or quoted under fair use.