Jump to content

[SOLVED] Get last row?


zeeshan_haider000

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.