zeeshan_haider000 Posted May 2, 2009 Share Posted May 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/ Share on other sites More sharing options...
semlabs Posted May 2, 2009 Share Posted May 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-824558 Share on other sites More sharing options...
zeeshan_haider000 Posted May 3, 2009 Author Share Posted May 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-824640 Share on other sites More sharing options...
zeeshan_haider000 Posted May 3, 2009 Author Share Posted May 3, 2009 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-824905 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 SELECT `Id`, `Name`, `EName`, `ENumber`, `Download1`, `Link`, `DateAdded` FROM `table` GROUP BY `Name` ORDER BY `ENumber` DESC LIMIT 1 ? Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-824988 Share on other sites More sharing options...
zeeshan_haider000 Posted May 3, 2009 Author Share Posted May 3, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-825064 Share on other sites More sharing options...
zeeshan_haider000 Posted May 3, 2009 Author Share Posted May 3, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-825089 Share on other sites More sharing options...
sungpeng Posted May 4, 2009 Share Posted May 4, 2009 hi can I know how to get the last "second" row only ORDER BY `listdate` DESC? Quote Link to comment https://forums.phpfreaks.com/topic/156586-solved-get-last-row/#findComment-825322 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.