Hi I Am Timbo Posted March 7, 2007 Share Posted March 7, 2007 I'm trying to write a query on a user table. The table looks like this: +--------+-----------+-----------+----------+----------------+ | idUser | username | firstname | lastname | originalIdUser | +--------+-----------+-----------+----------+----------------+ | 1 | tbarsness | Tim | Barsness | 1 | | 2 | asdf | Tim | Barsness | 1 | | 3 | tbarsness | Tim | Barsness | 1 | | 4 | rocks | Tim | Barsness | 1 | | 5 | johndoe | John | Doe | 5 | | 6 | agoobwa | John | agoobwa | 5 | | 7 | agoobwa | John | agoobwa | 5 | +--------+-----------+-----------+----------+----------------+ I am trying to get the whole row for group of originalIdUser having a max(idUser) The query I thought should work: SELECT * , max( idUser ) FROM `user` GROUP BY originalIdUser HAVING idUser = max( idUser ) But it returns an empty result set. This is because this query: SELECT * , max( idUser ) FROM `user` GROUP BY originalIdUser returns the first record in each group as below: +--------+-----------+-----------+----------+----------------+ | idUser | username | firstname | lastname | originalIdUser | +--------+-----------+-----------+----------+----------------+ | 1 | tbarsness | Tim | Barsness | 1 | | 5 | johndoe | John | Doe | 5 | +--------+-----------+-----------+----------+----------------+ This is what I wanted: +--------+-----------+-----------+----------+----------------+ | idUser | username | firstname | lastname | originalIdUser | +--------+-----------+-----------+----------+----------------+ | 4 | rocks | Tim | Barsness | 1 | | 7 | agoobwa | John | agoobwa | 5 | +--------+-----------+-----------+----------+----------------+ I am thinking I have to join the user table to itself, but I am not sure how. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/ Share on other sites More sharing options...
artacus Posted March 7, 2007 Share Posted March 7, 2007 Select your MAX(idUser) in a subquery Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201454 Share on other sites More sharing options...
Hi I Am Timbo Posted March 7, 2007 Author Share Posted March 7, 2007 He's right, this worked like a charm: SELECT * FROM `user` A WHERE idUser = ( SELECT MAX( idUser ) FROM user B WHERE A.originalIdUser = B.originalIdUser ) Thank you artacus Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201465 Share on other sites More sharing options...
btherl Posted March 7, 2007 Share Posted March 7, 2007 Thankyou Timbo for a perfectly worded question That's how artacus was able to answer it so easily. It's a relief after reading so many "Please help, my file doesn't send the file into the database" type questions.. Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201486 Share on other sites More sharing options...
artacus Posted March 7, 2007 Share Posted March 7, 2007 That's how artacus was able to answer it so easily. LOL. That and the summer internship I did with the psychic friends network REALLY comes in handy. Lucky for us, fiber optic cable channels psychic energy even better than copper. Wonder how old Dion is doing, haven't seen her in a while. Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201493 Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 Thankyou Timbo for a perfectly worded question That's how artacus was able to answer it so easily. It's a relief after reading so many "Please help, my file doesn't send the file into the database" type questions.. But isn't that a correlated subquery (I'd have to run explain to check for sure)? You should be able to use a derived table and get better performance, IMHO. Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201838 Share on other sites More sharing options...
artacus Posted March 7, 2007 Share Posted March 7, 2007 But isn't that a correlated subquery (I'd have to run explain to check for sure)? It is. What Fenway is saying is that since you are looking up all your users anyway, do the subquery in the FROM section so you can do two queries instead of n+1 queries in the WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201917 Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 But isn't that a correlated subquery (I'd have to run explain to check for sure)? It is. What Fenway is saying is that since you are looking up all your users anyway, do the subquery in the FROM section so you can do two queries instead of n+1 queries in the WHERE clause. It's worse than n+1... it's one extra query _for every row_ in the outer query! Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201923 Share on other sites More sharing options...
artacus Posted March 7, 2007 Share Posted March 7, 2007 Yeah, thats 1 query for the outer and n for the inner Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201926 Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 Oh, I get it... either way, it's not ideal. ;-) Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201930 Share on other sites More sharing options...
artacus Posted March 7, 2007 Share Posted March 7, 2007 You're right. But seeing as how Timbo actually figured it out on his own instead of asking "So what's a subquery? Can you write it for me?" [red]I[/red] wasn't going to crush his spirit. Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-201942 Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 Leave the crushing to me... lol. Quote Link to comment https://forums.phpfreaks.com/topic/41564-selecting-the-group-wise-maximum/#findComment-202020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.