{"id":5444,"date":"2024-11-14T13:32:25","date_gmt":"2024-11-14T05:32:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5444\/"},"modified":"2024-11-14T13:32:25","modified_gmt":"2024-11-14T05:32:25","slug":"mysql%e9%81%8d%e5%8e%86%e6%80%8e%e4%b9%88%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5444\/","title":{"rendered":"mysql\u904d\u5386\u600e\u4e48\u8bbe\u7f6e"},"content":{"rendered":"<blockquote><p>\n  \u904d\u5386 mysql \u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u6e38\u6807\uff1a\u4f7f\u7528declare\u3001open\u3001fetch\u3001while\u3001close\u8bed\u53e5\u9010\u884c\u83b7\u53d6\u548c\u5904\u7406\u6570\u636e\u3002\u8303\u56f4\u53d8\u91cf\uff1a\u4f7f\u7528\u53d8\u91cf\u6307\u5b9a\u6570\u636e\u8303\u56f4\uff0c\u901a\u8fc7where\u5b50\u53e5\u9010\u884c\u904d\u5386\u6570\u636e\u3002limit\u548coffset\u5b50\u53e5\uff1a\u5206\u9875\u904d\u5386\u6570\u636e\uff0climit\u6307\u5b9a\u884c\u6570\uff0coffset\u6307\u5b9a\u8df3\u8fc7\u7684\u884c\u6570\u3002\u4e34\u65f6\u8868\uff1a\u521b\u5efa\u4e34\u65f6\u8868\u5b58\u50a8\u8981\u904d\u5386\u7684\u6570\u636e\uff0c\u63d0\u9ad8\u904d\u5386\u5927\u91cf\u6570\u636e\u65f6\u7684\u6027\u80fd\u3002\u751f\u6210\u5668\uff1a\u8fd4\u56de\u53ef\u904d\u5386\u5bf9\u8c61\uff0c\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u65e0\u9700\u5b58\u50a8\u6574\u4e2a\u6570\u636e\u96c6\u5728\u5185\u5b58\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052813392615809.jpg\" class=\"aligncenter\" title=\"mysql\u904d\u5386\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" alt=\"mysql\u904d\u5386\u600e\u4e48\u8bbe\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u904d\u5386\u8bbe\u7f6e<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u904d\u5386\u6570\u636e\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a<\/p>\n<p><strong>1. \u5149\u6807\u6e38\u6807<\/strong><\/p>\n<p>\u6e38\u6807\u662f\u4e00\u79cd\u904d\u5386\u7ed3\u679c\u96c6\u7684\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u4e00\u6b21\u83b7\u53d6\u4e00\u884c\u6570\u636e\uff0c\u5e76\u6309\u884c\u79fb\u52a8\u6e38\u6807\u3002<\/p>\n<pre>DECLARE cursor_name CURSOR FOR SELECT * FROM table_name;\n\nOPEN cursor_name;\n\nFETCH cursor_name INTO variable1, variable2;\n\nWHILE cursor_name%FOUND DO\n  -- \u5904\u7406\u6570\u636e\n  FETCH cursor_name INTO variable1, variable2;\nEND WHILE;\n\nCLOSE cursor_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u8303\u56f4\u53d8\u91cf<\/strong><\/p>\n<p>\u8303\u56f4\u53d8\u91cf\u662f\u4e00\u79cd\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u53d8\u91cf\u6765\u6307\u5b9a\u6570\u636e\u8303\u56f4\u7684\u6280\u672f\u3002\u8fd9\u5141\u8bb8\u60a8\u9010\u884c\u904d\u5386\u6570\u636e\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u6e38\u6807\u3002<\/p>\n<pre>SELECT * FROM table_name\nWHERE id BETWEEN @start_id AND @end_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. LIMIT \u548c OFFSET \u5b50\u53e5<\/strong><\/p>\n<p>LIMIT \u548c OFFSET \u5b50\u53e5\u53ef\u7528\u4e8e\u5206\u9875\u904d\u5386\u6570\u636e\u3002LIMIT \u6307\u5b9a\u8981\u8fd4\u56de\u7684\u884c\u6570\uff0c\u800c OFFSET \u6307\u5b9a\u8981\u8df3\u8fc7\u7684\u884c\u6570\u3002<\/p>\n<pre>SELECT * FROM table_name\nLIMIT 10 OFFSET 20;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4e34\u65f6\u8868<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e34\u65f6\u8868\u6765\u5b58\u50a8\u8981\u904d\u5386\u7684\u6570\u636e\u3002\u8fd9\u53ef\u4ee5\u5728\u904d\u5386\u5927\u91cf\u6570\u636e\u65f6\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<pre>CREATE TEMPORARY TABLE temp_table AS\nSELECT * FROM table_name;\n\nSELECT * FROM temp_table;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u751f\u6210\u5668<\/strong><\/p>\n<p>\u751f\u6210\u5668\u662f\u4e00\u79cd Python \u4e2d\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u53ef\u904d\u5386\u7684\u5bf9\u8c61\u3002\u8fd9\u5141\u8bb8\u60a8\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u800c\u65e0\u9700\u5c06\u6574\u4e2a\u6570\u636e\u96c6\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002<\/p>\n<pre>def get_data():\n  with connection.cursor() as cursor:\n    cursor.execute(\"SELECT * FROM table_name\")\n    for row in cursor:\n      yield row<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u904d\u5386\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u89c4\u6a21\u3001\u6240\u9700\u7684\u6027\u80fd\u4ee5\u53ca\u5e94\u7528\u7a0b\u5e8f\u7684\u67b6\u6784\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u904d\u5386\u600e\u4e48\u8bbe\u7f6e\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>\u904d\u5386 mysql \u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u6e38\u6807\uff1a\u4f7f\u7528declare\u3001open\u3001fetch\u3001while\u3001close\u8bed\u53e5\u9010\u884c\u83b7\u53d6\u548c\u5904\u7406\u6570\u636e\u3002\u8303\u56f4\u53d8\u91cf\uff1a\u4f7f\u7528\u53d8\u91cf\u6307\u5b9a\u6570\u636e\u8303\u56f4\uff0c\u901a\u8fc7where\u5b50\u53e5\u9010\u884c\u904d\u5386\u6570\u636e\u3002limit\u548coffset\u5b50\u53e5\uff1a\u5206\u9875\u904d\u5386\u6570\u636e\uff0climit\u6307\u5b9a\u884c\u6570\uff0coffset\u6307\u5b9a\u8df3\u8fc7\u7684\u884c\u6570\u3002\u4e34\u65f6\u8868\uff1a\u521b\u5efa\u4e34\u65f6\u8868\u5b58\u50a8\u8981\u904d\u5386\u7684\u6570\u636e\uff0c\u63d0\u9ad8\u904d\u5386\u5927\u91cf\u6570\u636e\u65f6\u7684\u6027\u80fd\u3002\u751f\u6210\u5668\uff1a\u8fd4\u56de\u53ef\u904d\u5386\u5bf9\u8c61\uff0c\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u65e0\u9700\u5b58\u50a8\u6574\u4e2a\u6570\u636e\u96c6\u5728\u5185\u5b58\u4e2d\u3002 MySQL \u904d\u5386\u8bbe\u7f6e \u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u904d\u5386\u6570\u636e\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a 1. \u5149\u6807\u6e38\u6807 \u6e38\u6807\u662f\u4e00\u79cd\u904d\u5386\u7ed3\u679c\u96c6\u7684\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u4e00\u6b21\u83b7\u53d6\u4e00\u884c\u6570\u636e\uff0c\u5e76\u6309\u884c\u79fb\u52a8\u6e38\u6807\u3002 DECLARE cursor_name CURSOR FOR SELECT * FROM table_name; OPEN cursor_name; FETCH cursor_name INTO variable1, variable2; WHILE cursor_name%FOUND DO &#8212; \u5904\u7406\u6570\u636e FETCH cursor_name INTO variable1, variable2; END WHILE; CLOSE cursor_name; \u767b\u5f55\u540e\u590d\u5236 2. \u8303\u56f4\u53d8\u91cf \u8303\u56f4\u53d8\u91cf\u662f\u4e00\u79cd\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u53d8\u91cf\u6765\u6307\u5b9a\u6570\u636e\u8303\u56f4\u7684\u6280\u672f\u3002\u8fd9\u5141\u8bb8\u60a8\u9010\u884c\u904d\u5386\u6570\u636e\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u6e38\u6807\u3002 SELECT * FROM table_name WHERE id BETWEEN @start_id AND @end_id; \u767b\u5f55\u540e\u590d\u5236 [&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-5444","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5444","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=5444"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5444\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}