# SQL: getting the most recent row out of duplicate rows

**URL:** <https://forum.kirupa.com/t/sql-getting-the-most-recent-row-out-of-duplicate-rows/178875>\
**Category:** programming\
**Created:** [February 14, 2006, 11:34pm UTC](https://forum.kirupa.com/t/sql-getting-the-most-recent-row-out-of-duplicate-rows/178875 "2006-02-14T23:34:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![FreakSheep](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@FreakSheep](https://forum.kirupa.com/u/FreakSheep)\
**Post date:** [February 14, 2006, 11:34pm UTC](https://forum.kirupa.com/t/sql-getting-the-most-recent-row-out-of-duplicate-rows/178875/1 "2006-02-14T23:34:13Z")

</div>

Hey. I’m trying to get the most recent row from my table. I do have duplicate data in it, and thats whats throwing me off.

This is a basic veiw of what my table looks like…

timestamp ip FILE member  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959588+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959588+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959588 | 85.206.12.39 | /cp.php | 176  
[[URL=“http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959589+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php”]](http://66.98.252.78:2082/tbl_change.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&primary_key=+%60timestamp%60+%3D+1139959589&sql_query=SELECT+%2A+FROM+%60usersonline%60+WHERE+1&goto=sql.php)1139959589 | 81.202.86.237 | /url.php | 107  
[[URL=“http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959590+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php”]](http://66.98.252.78:2082/tbl_change.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&primary_key=+%60timestamp%60+%3D+1139959590&sql_query=SELECT+%2A+FROM+%60usersonline%60+WHERE+1&goto=sql.php)1139959590 | 82.252.48.145 | /contact.php | 138  
[[URL=“http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959594+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php”]](http://66.98.252.78:2082/tbl_change.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&primary_key=+%60timestamp%60+%3D+1139959594&sql_query=SELECT+%2A+FROM+%60usersonline%60+WHERE+1&goto=sql.php)1139959594 | 201.243.49.214 | /url.php | 180  
[[URL=“http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959599+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php”]](http://66.98.252.78:2082/tbl_change.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&primary_key=+%60timestamp%60+%3D+1139959599&sql_query=SELECT+%2A+FROM+%60usersonline%60+WHERE+1&goto=sql.php)1139959599 | 213.186.170.231 | /url.php | 177  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959600+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959600+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959600 | 83.44.117.220 | /faq.php | 182  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959601+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959601+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959601 | 85.206.12.39 | /url.php | 109  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959602+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959602+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959602 | 201.243.49.214 | /index.php | 120  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959604+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959604+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959604 | 85.206.12.39 | /url.php | 182  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959607+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959607+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959607 | 81.202.86.237 | /index.php | 107  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959608+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959608+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959608 | 85.206.12.39 | /url.php | 132  
[URL=“[http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation\_connection=utf8\_general\_ci&db=adcore&table=usersonline&pos=0&session\_max\_rows=30&disp\_direction=horizontal&repeat\_cells=100&dontlimitchars=0&sql\_query=DELETE+FROM+`usersonline`+WHERE+`timestamp`+%3D+1139959611+LIMIT+1&zero\_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation\_connection%3Dutf8\_general\_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session\_max\_rows%3D30%26disp\_direction%3Dhorizontal%26repeat\_cells%3D100%26dontlimitchars%3D0%26sql\_query%3DSELECT%2B%2A%2BFROM%2B%60usersonline%60%2BWHERE%2B1%26zero\_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl\_properties.php](http://66.98.252.78:2082/sql.php?lang=en-utf-8&server=1&collation_connection=utf8_general_ci&db=adcore&table=usersonline&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=0&sql_query=DELETE+FROM+%60usersonline%60+WHERE+%60timestamp%60+%3D+1139959611+LIMIT+1&zero_rows=The+row+has+been+deleted&goto=sql.php%3Flang%3Den-utf-8%26server%3D1%26collation_connection%3Dutf8_general_ci%26db%3Dadcore%26table%3Dusersonline%26pos%3D0%26session_max_rows%3D30%26disp_direction%3Dhorizontal%26repeat_cells%3D100%26dontlimitchars%3D0%26sql_query%3DSELECT%2B%252A%2BFROM%2B%2560usersonline%2560%2BWHERE%2B1%26zero_rows%3DThe%2Brow%2Bhas%2Bbeen%2Bdeleted%26goto%3Dtbl_properties.php)”]1139959611 | 213.186.170.231 | /register.php | 174

ok, so what i’m trying to do, is getting what file which ip is currently on, so i need to fine the MAX(timestamp) for each ip, and get the FILE field, but i running into trouble.

This is the command im using now…  
SELECT MAX( timestamp ) , FILE , member, ip FROM usersonline GROUP BY ip ORDER BY timestamp DESC

but its gettign the max(timestamp), but not the FILE that goes along with that timestamp.

Thanks.  
\_naaman
