{"id":2029,"date":"2013-06-19T06:42:26","date_gmt":"2013-06-19T06:42:26","guid":{"rendered":"http:\/\/www.withdata.com\/ad\/?p=2029"},"modified":"2017-04-20T11:05:16","modified_gmt":"2017-04-20T11:05:16","slug":"fetch-the-row-which-has-the-max-value-for-a-column-on-mysql","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/mysql\/fetch-the-row-which-has-the-max-value-for-a-column-on-mysql.html","title":{"rendered":"Fetch the row which has the Max value for a column on Mysql"},"content":{"rendered":"<p>I have a <a href=\"https:\/\/www.mysql.com\/\">MySQL<\/a> table clientipstat, 3 columns: barcode, statdate, count.<\/p>\n<p>I want to get the latest row for each barcode(1 result per unique barcode), like this:<\/p>\n<pre>barcode\t\tstatdate\tcount\r\n1525020018\t2013\/5\/9\t1\r\n1505080034\t2013\/5\/11\t1\r\n6205020011\t2013\/5\/15\t1\r\n6205020026\t2013\/5\/17\t1\r\n6205030040\t2013\/5\/18\t2<\/pre>\n<p>I use MAX() + GROUP BY to do this:<\/p>\n<pre>select c.barcode,c.statdate,c.count from clientipstat c \r\ninner join \r\n(select barcode, max(statdate) as last_statdate\r\n\tfrom clientipstat\r\n\tgroup by barcode) maxt\r\non (c.barcode = maxt.barcode and c.statdate = maxt.last_statdate)<\/pre>\n<p>Some MySQL tools you can try: <a href=\"https:\/\/www.withdata.com\/mysql\/\"> https:\/\/www.withdata.com\/mysql\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a MySQL table clientipstat, 3 columns: barcode, statdate, count. I want to get the latest row for each barcode(1 result per unique barcode), like this: barcode statdate count 1525020018 2013\/5\/9 1 1505080034 2013\/5\/11 1 6205020011 2013\/5\/15 1 6205020026 2013\/5\/17 1 6205030040 2013\/5\/18 2 I use MAX() + GROUP BY to do this: select [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[271,1323],"class_list":["post-2029","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-max-group-by","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/2029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=2029"}],"version-history":[{"count":2,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/2029\/revisions"}],"predecessor-version":[{"id":6449,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/2029\/revisions\/6449"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=2029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=2029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=2029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}