{"id":5226,"date":"2024-11-14T12:11:19","date_gmt":"2024-11-14T04:11:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5226\/"},"modified":"2024-11-14T12:11:19","modified_gmt":"2024-11-14T04:11:19","slug":"mysql%e8%87%aa%e5%a2%9eid%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5226\/","title":{"rendered":"mysql\u81ea\u589eid\u600e\u4e48\u83b7\u53d6"},"content":{"rendered":"<blockquote><p>\n  \u83b7\u53d6 mysql \u81ea\u589e id \u6709\u4e24\u79cd\u65b9\u6cd5\uff1a1.\u4f7f\u7528 last_insert_id() \u51fd\u6570\u8fd4\u56de\u6700\u8fd1\u63d2\u5165\u8bb0\u5f55\u7684\u81ea\u589e id\uff1b2. \u4f7f\u7528 insert &#8230; returning \u8bed\u53e5\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u540c\u65f6\u68c0\u7d22\u81ea\u589e id\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052617012624284.jpg\" class=\"aligncenter\" title=\"mysql\u81ea\u589eid\u600e\u4e48\u83b7\u53d6\u63d2\u56fe\" alt=\"mysql\u81ea\u589eid\u600e\u4e48\u83b7\u53d6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6 MySQL \u81ea\u589e ID<\/strong><\/p>\n<p>MySQL \u81ea\u589e ID \u662f\u4e00\u4e2a\u5728\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u81ea\u52a8\u9012\u589e\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u83b7\u53d6\u81ea\u589e ID \u6709\u4ee5\u4e0b\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 LAST_INSERT_ID() \u51fd\u6570<\/strong><\/p>\n<p>LAST_INSERT_ID() \u51fd\u6570\u8fd4\u56de\u6700\u8fd1\u63d2\u5165\u8bb0\u5f55\u7684\u81ea\u589e ID\u3002\u5b83\u901a\u5e38\u4e0e INSERT \u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>INSERT INTO table_name (name, age) VALUES ('John', 25);\nSELECT LAST_INSERT_ID();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 INSERT &#8230; RETURNING \u8bed\u53e5<\/strong><\/p>\n<p>INSERT &#8230; RETURNING \u8bed\u53e5\u5141\u8bb8\u60a8\u5728\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u68c0\u7d22\u63d2\u5165\u7684\u81ea\u589e ID\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>INSERT INTO table_name (name, age) VALUES ('John', 25) RETURNING id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d &#8220;id&#8221; \u662f\u81ea\u589e ID \u5217\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\u8868\uff1a<\/p>\n<pre>CREATE TABLE people (\n  id INT NOT NULL AUTO_INCREMENT,\n  name VARCHAR(255),\n  age INT\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8981\u83b7\u53d6\u63d2\u5165\u65b0\u8bb0\u5f55\u7684\u81ea\u589e ID\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a<\/strong><\/p>\n<pre>INSERT INTO people (name, age) VALUES ('John', 25);\nSELECT LAST_INSERT_ID();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u65b0\u63d2\u5165\u8bb0\u5f55\u7684 ID\u3002<\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a<\/strong><\/p>\n<pre>INSERT INTO people (name, age) VALUES ('John', 25) RETURNING id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u63d2\u5165\u7684\u81ea\u589e ID\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u81ea\u589eid\u600e\u4e48\u83b7\u53d6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u83b7\u53d6 mysql \u81ea\u589e id \u6709\u4e24\u79cd\u65b9\u6cd5\uff1a1.\u4f7f\u7528 last_insert_id() \u51fd\u6570\u8fd4\u56de\u6700\u8fd1\u63d2\u5165\u8bb0\u5f55\u7684\u81ea\u589e id\uff1b2. \u4f7f\u7528 insert &#8230; returning \u8bed\u53e5\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u540c\u65f6\u68c0\u7d22\u81ea\u589e id\u3002 \u5982\u4f55\u83b7\u53d6 MySQL \u81ea\u589e ID MySQL \u81ea\u589e ID \u662f\u4e00\u4e2a\u5728\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u81ea\u52a8\u9012\u589e\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u83b7\u53d6\u81ea\u589e ID \u6709\u4ee5\u4e0b\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a \u65b9\u6cd5 1\uff1a\u4f7f\u7528 LAST_INSERT_ID() \u51fd\u6570 LAST_INSERT_ID() \u51fd\u6570\u8fd4\u56de\u6700\u8fd1\u63d2\u5165\u8bb0\u5f55\u7684\u81ea\u589e ID\u3002\u5b83\u901a\u5e38\u4e0e INSERT \u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u5982\u4e0b\u6240\u793a\uff1a INSERT INTO table_name (name, age) VALUES (&#8216;John&#8217;, 25); SELECT LAST_INSERT_ID(); \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 INSERT &#8230; RETURNING \u8bed\u53e5 INSERT &#8230; RETURNING \u8bed\u53e5\u5141\u8bb8\u60a8\u5728\u63d2\u5165\u65b0\u8bb0\u5f55\u65f6\u68c0\u7d22\u63d2\u5165\u7684\u81ea\u589e ID\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5226","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5226"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5226\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}