{"id":5503,"date":"2024-11-14T09:34:45","date_gmt":"2024-11-14T01:34:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5503\/"},"modified":"2024-11-14T09:34:45","modified_gmt":"2024-11-14T01:34:45","slug":"mysql%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96insert%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5503\/","title":{"rendered":"mysql\u600e\u4e48\u83b7\u53d6insert\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u83b7\u53d6\u63d2\u5165\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 last_insert_id() \u51fd\u6570\u83b7\u53d6\u6700\u8fd1\u63d2\u5165\u884c\u7684 id\u3002\u4f7f\u7528 select &#8230; returning \u8bed\u53e5\u5728\u63d2\u5165\u65f6\u68c0\u7d22\u63d2\u5165\u884c\u7684\u503c\u3002\u4f7f\u7528 information_schema.table_statistics \u8868\u67e5\u8be2\u8868\u4e2d\u63d2\u5165\u7684\u884c\u6570\u3002\u4f7f\u7528\u89e6\u53d1\u5668\u5728\u63d2\u5165\u64cd\u4f5c\u65f6\u81ea\u52a8\u6267\u884c\u52a8\u4f5c\uff0c\u68c0\u7d22\u548c\u8bb0\u5f55\u63d2\u5165\u7684\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052905092474634.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u83b7\u53d6insert\u6570\u636e\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u83b7\u53d6insert\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u83b7\u53d6\u63d2\u5165\u6570\u636e<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\u83b7\u53d6\u63d2\u5165\u6570\u636e\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 LAST_INSERT_ID() \u51fd\u6570<\/strong><\/p>\n<p>LAST_INSERT_ID() \u51fd\u6570\u4f1a\u8fd4\u56de\u6700\u8fd1\u6267\u884c\u7684 INSERT \u8bed\u53e5\u4e2d\u63d2\u5165\u884c\u7684 ID\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT LAST_INSERT_ID();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 SELECT &#8230; RETURNING \u8bed\u53e5<\/strong><\/p>\n<p>SELECT &#8230; RETURNING \u8bed\u53e5\u53ef\u4ee5\u8ba9\u60a8\u5728\u6267\u884c INSERT \u8bed\u53e5\u7684\u540c\u65f6\u68c0\u7d22\u63d2\u5165\u884c\u7684\u503c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>INSERT INTO table_name (column1, column2, ...)\nVALUES (value1, value2, ...)\nRETURNING column1, column2, ...;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 information_schema.table_statistics \u8868<\/strong><\/p>\n<p>information_schema.table_statistics \u8868\u5b58\u50a8\u4e86\u8868\u4e2d\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u5305\u62ec\u63d2\u5165\u7684\u884c\u6570\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>SELECT INSERT_ROWS\nFROM information_schema.table_statistics\nWHERE TABLE_SCHEMA = database_name\nAND TABLE_NAME = table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u89e6\u53d1\u5668<\/strong><\/p>\n<p>\u89e6\u53d1\u5668\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5bf9\u8c61\uff0c\u5f53\u5bf9\u8868\u8fdb\u884c\u7279\u5b9a\u64cd\u4f5c\uff08\u5982\u63d2\u5165\uff09\u65f6\uff0c\u4f1a\u81ea\u52a8\u6267\u884c\u4e00\u4e9b\u52a8\u4f5c\u3002\u53ef\u4ee5\u521b\u5efa\u89e6\u53d1\u5668\u6765\u68c0\u7d22\u548c\u8bb0\u5f55\u63d2\u5165\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>LAST_INSERT_ID() \u51fd\u6570\u53ea\u8fd4\u56de\u6700\u8fd1\u4e00\u6b21\u63d2\u5165\u884c\u7684 ID\uff0c\u800c SELECT &#8230; RETURNING \u548c information_schema.table_statistics \u65b9\u6cd5\u53ef\u4ee5\u68c0\u7d22\u591a\u884c\u6570\u636e\u3002<\/li>\n<li>information_schema.table_statistics \u8868\u4e2d\u7684\u6570\u636e\u53ef\u80fd\u4f1a\u8fc7\u671f\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u7528\u4e8e\u5b9e\u65f6\u6570\u636e\u68c0\u7d22\u3002<\/li>\n<li>\u89e6\u53d1\u5668\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u8bbe\u7f6e\u8d77\u6765\u4e5f\u66f4\u590d\u6742\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u83b7\u53d6insert\u6570\u636e\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>\u5728 mysql \u4e2d\u83b7\u53d6\u63d2\u5165\u6570\u636e\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 last_insert_id() \u51fd\u6570\u83b7\u53d6\u6700\u8fd1\u63d2\u5165\u884c\u7684 id\u3002\u4f7f\u7528 select &#8230; returning \u8bed\u53e5\u5728\u63d2\u5165\u65f6\u68c0\u7d22\u63d2\u5165\u884c\u7684\u503c\u3002\u4f7f\u7528 information_schema.table_statistics \u8868\u67e5\u8be2\u8868\u4e2d\u63d2\u5165\u7684\u884c\u6570\u3002\u4f7f\u7528\u89e6\u53d1\u5668\u5728\u63d2\u5165\u64cd\u4f5c\u65f6\u81ea\u52a8\u6267\u884c\u52a8\u4f5c\uff0c\u68c0\u7d22\u548c\u8bb0\u5f55\u63d2\u5165\u7684\u6570\u636e\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u83b7\u53d6\u63d2\u5165\u6570\u636e \u5728 MySQL \u4e2d\u83b7\u53d6\u63d2\u5165\u6570\u636e\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 LAST_INSERT_ID() \u51fd\u6570 LAST_INSERT_ID() \u51fd\u6570\u4f1a\u8fd4\u56de\u6700\u8fd1\u6267\u884c\u7684 INSERT \u8bed\u53e5\u4e2d\u63d2\u5165\u884c\u7684 ID\u3002 \u8bed\u6cd5\uff1a SELECT LAST_INSERT_ID(); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 SELECT &#8230; RETURNING \u8bed\u53e5 SELECT &#8230; RETURNING \u8bed\u53e5\u53ef\u4ee5\u8ba9\u60a8\u5728\u6267\u884c INSERT \u8bed\u53e5\u7684\u540c\u65f6\u68c0\u7d22\u63d2\u5165\u884c\u7684\u503c\u3002 \u8bed\u6cd5\uff1a INSERT INTO table_name (column1, column2, &#8230;) VALUES (value1, value2, &#8230;) RETURNING [&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-5503","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5503","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=5503"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5503\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}