CodingMySQL GROUP BY headspafe

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Kenny J (WINGNUTKJ)  
 To:  ALL
30962.1 
I'm currently attempting to write my first WordPress plugin, which is the first time I've really used PHP and MySQL, other than to do some basic cleaning up of other people's stuff.

I'm currently trying to write a query that'll bring back the titles of all the posts, plus the ID of the highest-IDed comment for that post (or null, if there's none). I've got:

SQL code:
SELECT kj_posts.ID, kj_posts.post_date, max( kj_comments.comment_ID ) , kj_posts.post_title
FROM kj_posts
LEFT  OUTER  JOIN kj_comments ON kj_comments.comment_post_id = kj_posts.ID
WHERE 1=1
GROUP  BY kj_comments.comment_post_id


It almost works - for posts that have comments, it behaves as I want it to, but it only returns one of the posts that don't have any comments. Does MySQL have any clevers to let me do what I want, or will I have to dump the OUTER JOIN stuff and just bung a subquery into the SELECT clause?

Kenny
The Wisdom of Amazon customer reviews:
Kurt Vonnegut: Slaughterhouse 5
My main problem, apart from the uber-randomness, is that the events in the book didn't actually happen, and the main character didn't actually go through the war, because he didn't exist. I'm a freshman in high school, and I was relieved when my English G/T (Lyceum) teacher confirmed my suspiciouns that Kurt Vonnegut was, in fact, high.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Kenny J (WINGNUTKJ)     
30962.2 In reply to 30962.1 
It works for posts with comments because you're grouping only the posts that have comments.

When you've got an aggregate function in the select (ie: SUM/MAX/AVG/etc) then you group by all other fields in the select.

ie:
code:
GROUP BY kj_posts.ID, kj_posts.post_date,  kj_posts.post_title


Also, you shouldn't need that WHERE 1=1 (unless you have a fancy set of PHP if conditions hiding a selection of AND/ORs under it)
0/0
 Reply   Quote More 

 From:  Kenny J (WINGNUTKJ)  
 To:  Peter (BOUGHTONP)     
30962.3 In reply to 30962.2 

Cheers! It's been a while since I've written any SQL, and it appears my head has forgotten some stuff.

 

The WHERE 1=1 thing is just a placeholder for when I put in the actual WHERE clause later. Because otherwise I'll forget.


Kenny
The Wisdom of Amazon customer reviews:
Kurt Vonnegut: Slaughterhouse 5
My main problem, apart from the uber-randomness, is that the events in the book didn't actually happen, and the main character didn't actually go through the war, because he didn't exist. I'm a freshman in high school, and I was relieved when my English G/T (Lyceum) teacher confirmed my suspiciouns that Kurt Vonnegut was, in fact, high.
0/0
 Reply   Quote More 

 From:  Matt  
 To:  Kenny J (WINGNUTKJ)     
30962.4 In reply to 30962.1 
What Pete said.

Except, if kj_posts.ID is the primary key then it will be sufficient enough to group on kj_posts.ID alone and leave out the other columns as Pete listed as adding them to the query is being wasteful by asking MySQL to perform grouping on data which will result in no change to the results.

So Query in full:

sql code:
SELECT kj_posts.ID, kj_posts.post_date, max( kj_comments.comment_ID), 
kj_posts.post_title FROM kj_posts LEFT  JOIN kj_comments ON 
kj_comments.comment_post_id = kj_posts.ID GROUP BY kj_posts.ID 
ORDER BY kj_posts.post_date

doohicky

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Matt     
30962.5 In reply to 30962.4 
Is that a MySQL feature or part of standard SQL?
0/0
 Reply   Quote More 

 From:  Kenny J (WINGNUTKJ)  
 To:  Matt     
30962.6 In reply to 30962.4 
Cunning! Cheers.

Kenny
The Wisdom of Amazon customer reviews:
Kurt Vonnegut: Slaughterhouse 5
My main problem, apart from the uber-randomness, is that the events in the book didn't actually happen, and the main character didn't actually go through the war, because he didn't exist. I'm a freshman in high school, and I was relieved when my English G/T (Lyceum) teacher confirmed my suspiciouns that Kurt Vonnegut was, in fact, high.
0/0
 Reply   Quote More 

 From:  Matt  
 To:  Peter (BOUGHTONP)     
30962.7 In reply to 30962.5 

Standard SQL I would have though.

 

The primary key requires the column to be unique so grouping on other columns becomes redudant because there is no duplicate data.

doohicky

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Matt     
30962.8 In reply to 30962.7 
Makes sense.

But I often get errors at work when neglecting to add a fieldname to a group by bit... I guess it is possible that I only get them when not using the [whole] primary key. Which is probably more likely thinking about it - most group by stuff there is likely to be reports, when individual data isn't wanted, so would be grouping on less specific stuff.
Would be nice if there was a shortcut for 'everything except the function' though.
0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  Matt     
30962.9 In reply to 30962.4 
presumably the optimiser in mysql will result in the queries being the same though?!
0/0
 Reply   Quote More 

 From:  Ally  
 To:  Peter (BOUGHTONP)     
30962.10 In reply to 30962.8 
The only time you should get an error is if you try and output anything that isn't SUMed, COUNTed, or GROUPed (or the other ones I forget)
0/0
 Reply   Quote More 

 From:  Matt  
 To:  Ben (BENLUMLEY)     
30962.11 In reply to 30962.9 

I was under the impression that the query optimiser rather than scan the query to know how to eliminate the unrequired modifiers, it simply reorgansied the query internally so it was more efficient in how it opened and closed tables and read from keys and such like.

 

After all without running the query multiple times it wouldn't know if the modifiers made a difference or not to the results would it?

doohicky

0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  Matt     
30962.12 In reply to 30962.11 

i don't really know what it does and doesn't do ... just that it uses clevers to recognise queries that can be better performed differently, without actually trying the different versions.

 

I was under the impression that it did things like notice that one of the items in a group by is a unique index, so would ignore the rest?

 

No idea how i got this idea though, so its most probably wrong.

0/0
 Reply   Quote More 

Reply to All    
 

1–12

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats