{"id":56,"date":"2007-07-05T10:56:09","date_gmt":"2007-07-05T16:56:09","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=56"},"modified":"2007-07-05T10:56:09","modified_gmt":"2007-07-05T16:56:09","slug":"messing-with-last_insert_id","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=56","title":{"rendered":"Messing with LAST_INSERT_ID()"},"content":{"rendered":"<p>Time for another MySQL Community Quiz:<\/p>\n<p>Everybody knows that LAST_INSERT_ID() returns the value most recently created in an auto_increment column. Fewer of us know that you can pass a value to LAST_INSERT_ID(), and that value will be returned for the next call to LAST_INSERT_ID().  For example,<\/p>\n<pre>INSERT INTO table (someNonAutoIncrementField) VALUES (LAST_INSERT_ID(42));\r\nSELECT LAST_INSERT_ID();<\/pre>\n<p>The INSERT inserts &#8217;42&#8217; into the table, and the SELECT returns 42.<\/p>\n<p>So, here&#8217;s the question: What if we pass an expression to LAST_INSERT_ID and also use an auto_increment field in the same statement?<\/p>\n<pre>CREATE TABLE test (id int auto_increment primary key, field int);\r\nINSERT INTO test (id, field) VALUES (NULL, LAST_INSERT_ID(42));\r\nSELECT LAST_INSERT_ID();<\/pre>\n<p><spoiler 'Show Answer' 'Hide Answer'><br \/>\nYou get the auto_increment field, at least in this case:<\/p>\n<pre>mysql> INSERT INTO test (id, field) VALUES (NULL, LAST_INSERT_ID(42));\r\nQuery OK, 1 row affected (0.08 sec)\r\n\r\nmysql> SELECT LAST_INSERT_ID();\r\n+------------------+\r\n| LAST_INSERT_ID() |\r\n+------------------+\r\n|                1 |\r\n+------------------+\r\n1 row in set (0.00 sec)<\/pre>\n<p><\/spoiler><\/p>\n<p>And for a follow up, does it matter if you swap the insertion around?<\/p>\n<pre>INSERT INTO test (field, id) VALUES (LAST_INSERT_ID(42), NULL);\r\nSELECT LAST_INSERT_ID();<\/pre>\n<p><spoiler 'Show Answer' 'Hide Answer'><br \/>\nNope.<\/p>\n<pre>mysql> INSERT INTO test (field, id) VALUES (LAST_INSERT_ID(42), NULL);\r\nQuery OK, 1 row affected (0.08 sec)\r\n\r\nmysql> SELECT LAST_INSERT_ID();\r\n+------------------+\r\n| LAST_INSERT_ID() |\r\n+------------------+\r\n|                2 |\r\n+------------------+\r\n1 row in set (0.00 sec)<\/pre>\n<p><\/spoiler><\/p>\n<p>The manual warns about mixing LAST_INSERT_ID() and LAST_INSERT_ID(expr). This may fall under that same caveat.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time for another MySQL Community Quiz: Everybody knows that LAST_INSERT_ID() returns the value most recently created in an auto_increment column. Fewer of us know that you can pass a value to LAST_INSERT_ID(), and that value will be returned for the next call to LAST_INSERT_ID(). For example, INSERT INTO table (someNonAutoIncrementField) VALUES (LAST_INSERT_ID(42)); SELECT LAST_INSERT_ID(); The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-gotchas"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-U","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":0,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}