{"id":558,"date":"2015-11-11T22:03:21","date_gmt":"2015-11-11T21:03:21","guid":{"rendered":"http:\/\/www.moraleslada.com\/blog\/rafael\/?p=558"},"modified":"2019-02-18T16:44:40","modified_gmt":"2019-02-18T15:44:40","slug":"trucos-para-mysql-ultimo-registro-de-una-tabla","status":"publish","type":"post","link":"http:\/\/www.moraleslada.com\/?p=558","title":{"rendered":"Trucos para MySQL &#8211; \u00daltimo registro de una tabla"},"content":{"rendered":"<p><a href=\"http:\/\/www.moraleslada.com\/blog\/rafael\/wp-content\/uploads\/2015\/11\/mysql-logo900.jpg\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-559\" src=\"http:\/\/www.moraleslada.com\/blog\/rafael\/wp-content\/uploads\/2015\/11\/mysql-logo900-300x133.jpg\" alt=\"mysql-logo900\" width=\"300\" height=\"133\" srcset=\"http:\/\/www.moraleslada.com\/wp-content\/uploads\/2015\/11\/mysql-logo900-300x133.jpg 300w, http:\/\/www.moraleslada.com\/wp-content\/uploads\/2015\/11\/mysql-logo900.jpg 900w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Obtener \u00faltimo registro de una tabla:<\/strong><\/p>\n<p>select * from tabla order by id desc limit 1<\/p>\n<p><strong>Obtener \u00faltimo \u00abid\u00bb&nbsp; (registro) de una tabla:<\/strong><\/p>\n<p>select max(id) from tabla<\/p>\n<p>&#8212;&#8212;&#8211;<\/p>\n<p><code>$rs = mysql_query(\"SELECT @@identity AS id\");<br \/>\nif ($row = mysql_fetch_row($rs)) {<br \/>\n$id = trim($row[0]);<br \/>\n}<\/code><\/p>\n<p>Devuelve el id del ultimo ingreso por lo que se recomienda utilizar a continuaci\u00f3n de la sentencia INSERT.<\/p>\n<p>&#8212;&#8212;&#8212;<\/p>\n<p><code>$rs = mysql_query(\"SELECT MAX(id_tabla) AS id FROM tabla\");<br \/>\nif ($row = mysql_fetch_row($rs)) {<br \/>\n$id = trim($row[0]);<br \/>\n}<\/code><\/p>\n<p>S\u00f3lo funciona en campos con la propiedad auto-increment.<\/p>\n<p>La ventaja de este metodo con respecto al anterior es que puede ser utilizado incluso si se han realizado otras inserciones a otras tablas, si se ha reiniciado el computador, etc;&nbsp; ya que este metodo busca el id de mayor valor dentro de la tabla y como es auto incrementable el mayor siempre sera el \u00faltimo. Es muy \u00fatil si vas a recuperar el \u00faltimo id en cualquier momento y no s\u00f3lo despues de insertarlo.<\/p>\n<p>&#8212;&#8212;&#8212;<\/p>\n<p><code>$id=mysql_insert_id();<\/code><\/p>\n<p>Al igual que la propiedad @@identity, solo retorna el id del \u00faltimo registro ingresado independientemente de la tabla en la que se haya realizado la operaci\u00f3n.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Obtener \u00faltimo registro de una tabla: select * from tabla order by id desc limit 1 Obtener \u00faltimo \u00abid\u00bb&nbsp; (registro) de una tabla: select max(id) from tabla &#8212;&#8212;&#8211; $rs = mysql_query(\u00abSELECT @@identity AS id\u00bb); if ($row = mysql_fetch_row($rs)) { $id = trim($row[0]); } Devuelve el id del ultimo ingreso por lo que se recomienda utilizar [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":""},"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=\/wp\/v2\/posts\/558"}],"collection":[{"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=558"}],"version-history":[{"count":2,"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=\/wp\/v2\/posts\/558\/revisions"}],"predecessor-version":[{"id":618,"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=\/wp\/v2\/posts\/558\/revisions\/618"}],"wp:attachment":[{"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=558"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moraleslada.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}