Jump to content

Recommended Posts

Hi,

How do you get the last row from a group with a GROUP By clause?

This is the query: "Select Id, Name, EName, ENumber, Download1, Link, DateAdded FROM table GROUP BY Name"

I have some data like:

 

Name: AAA

ENumber: 102

Name: AAA

ENumber: 109

 

Name: BBB

Enumber: 103

Name: BBB

Enumber: 234

 

I want the result grouped by Ename AND max Enumber (like AAA, 109 instead of AAA, 102)..

I can't figure out how to do this...

Help would be much appreciated..

Zee

Link to comment
https://forums.phpfreaks.com/topic/156586-solved-get-last-row/
Share on other sites

Just order it by ename and limit it to 1:

 

Select Id, Name, EName, ENumber, Download1, Link, DateAdded FROM table ORDER BY EName DESC LIMIT 1

 

That doesn't work because there are different Enames with multiple Enumbers, and i want the last Enumbers for all the individual Enames, so you can't omit GROUP BY clause.

Link to comment
https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-824640
Share on other sites

SELECT `Id`, `Name`, `EName`, `ENumber`, `Download1`, `Link`, `DateAdded`
FROM `table`
GROUP BY `Name`
ORDER BY `ENumber` DESC
LIMIT 1

 

?

 

I already tried that, when you use LIMIT 1, you are limiting the results to only one row. What i want is to get rows with DISTINCT Enames + in those Enames i want to get the last Enumber... and i don't know if it's possible using a query?

Link to comment
https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-825064
Share on other sites

Well i just figured how to do what i wanted to do.

if your ID field are auto incrementing or any other field that is INT, you can use this:

 

SELECT data.* FROM data INNER JOIN (SELECT MAX(id) AS id FROM data group by url) ids ON data.id = ids.id

 

to get a unique Field1 with last inserted data Field2..

Link to comment
https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-825089
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.