{"id":147,"date":"2024-07-01T22:43:58","date_gmt":"2024-07-01T14:43:58","guid":{"rendered":"https:\/\/www.jiwenbo.top\/?p=147"},"modified":"2024-07-01T22:43:58","modified_gmt":"2024-07-01T14:43:58","slug":"mysql%e5%9f%ba%e7%a1%80%e7%af%87-dml","status":"publish","type":"post","link":"https:\/\/jiwenbo.top\/index.php\/2024\/07\/01\/mysql%e5%9f%ba%e7%a1%80%e7%af%87-dml\/","title":{"rendered":"MySql\u57fa\u7840\u7bc7\uff1aDML\u7bc7"},"content":{"rendered":"\n<p>DML:\u6570\u636e\u64cd\u7eb5\u8bed\u53e5\uff0c\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u5e93\u5bf9\u8c61\u4e2d\u6240\u5305\u542b\u7684\u6570\u636e<\/p>\n\n\n\n<p>\u4e00.\u6dfb\u52a0\u6570\u636e\uff1aINSERT<\/p>\n\n\n\n<p>1.\u63d2\u5165\u5355\u6761\u6570\u636e<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">insert into \u8868\u540d(\u5217\u540d1,\u5217\u540d2,...) values(\u503c1,\u503c2,...);<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fd0000\" class=\"has-inline-color\">\u6ce8\u610f<\/mark>\uff1a\u63d2\u5165\u503c\u7684\u7c7b\u578b\u8981\u4e0e\u5217\u7684\u7c7b\u578b\u4e00\u81f4\u6216\u517c\u5bb9\uff0c\u63d2\u5165NULL\u53ef\u5b9e\u73b0\u4e3a\u5217\u63d2\u5165NULL\u503c\u3002<br>\u5217\u7684\u987a\u5e8f\u53ef\u4ee5\u8c03\u6362\u3002\u5217\u6570\u548c\u503c\u7684\u4e2a\u6570\u5fc5\u987b\u4e00\u81f4\u3002\u53ef\u7701\u7565\u5217\u540d\uff0c\u9ed8\u8ba4\u6240\u6709\u5217\uff0c\u5e76\u4e14\u5217\u7684\u987a\u5e8f\u548c\u8868\u4e2d\u5217\u7684\u987a\u5e8f\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>2. \u63d2\u5165\u5355\u6761\u6570\u636e<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">INSERT INTO \u8868\u540d SET \u5217\u540d = \u503c,\u5217\u540d = \u503c;<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6bcf\u6b21\u53ea\u80fd\u63d2\u5165\u4e00\u884c\u6570\u636e\uff0c\u6bcf\u5217\u7684\u503c\u901a\u8fc7\u8d4b\u503c\u5217\u8868\u5236\u5b9a\u3002<\/p>\n\n\n\n<p>3. \u63d2\u5165\u591a\u6761\u6570\u636e<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">insert into \u8868\u540d values(\u503c1,\u503c2,\u503c3),(\u503c4,\u503c5,\u503c6),(\u503c7,\u503c8,\u503c9);<\/pre>\n\n\n\n<p>\u5982\u679c\u53ea\u60f3\u90e8\u5206\u63d2\u5165\u6570\u636e\uff0c\u9700\u8981\u5728 Value List \u4e4b\u524d\u52a0\u4e0a ColumnName List\uff0c<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">insert into \u8868\u540d (\u5217\u540d1,\u5217\u540d2) values(\u503c1,\u503c2);<\/pre>\n\n\n\n<p>\u9664\u4e86\u5217\u540d1\u548c\u5217\u540d2\u8fd9\u4e24\u5217\u7684\u503c\uff0c\u5176\u4ed6\u5217\u7684\u503c\u4f1a\u8bbe\u4e3a Null\u3002<\/p>\n\n\n\n<p>4. \u8868\u6570\u636e\u590d\u5236<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">INSERT INTO \u8868\u540d SELECT * from \u8868\u540d;<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\u6ce8\u610f<\/mark>\uff1a\u4e24\u4e2a\u8868\u7684\u5b57\u6bb5\u9700\u8981\u4e00\u76f4\uff0c\u5e76\u5c3d\u91cf\u4fdd\u8bc1\u8981\u65b0\u589e\u7684\u8868\u4e2d\u6ca1\u6709\u6570\u636e<\/p>\n\n\n\n<p>\u4e8c.\u66f4\u65b0\u6570\u636e\uff1aUPDATE<\/p>\n\n\n\n<p>Update \u8bed\u53e5\u4e00\u5171\u6709\u4e24\u79cd\u8bed\u6cd5\uff0c\u5206\u522b\u7528\u4e8e\u66f4\u65b0\u5355\u8868\u6570\u636e\u548c\u591a\u8868\u6570\u636e\u3002<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\u6ce8\u610f<\/mark>\uff1a\u6ca1\u6709 WHERE \u6761\u4ef6\u7684 UPDATE \u4f1a\u66f4\u65b0\u6240\u6709\u503c\uff01<\/p>\n\n\n\n<p>1. \u4fee\u6539\u4e00\u6761\u6570\u636e\u7684\u67d0\u4e2a\u5b57\u6bb5<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">UPDATE \u8868\u540d SET \u5b57\u6bb5\u540d =\u503c where \u5b57\u6bb5\u540d=\u503c<\/pre>\n\n\n\n<p>2. \u4fee\u6539\u591a\u4e2a\u5b57\u6bb5\u4e3a\u540c\u4e00\u7684\u503c<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">UPDATE \u8868\u540d SET \u5b57\u6bb5\u540d= \u503c WHERE \u5b57\u6bb5\u540d in ('\u503c1','\u503c2','\u503c3');<\/pre>\n\n\n\n<p>3. \u4f7f\u7528case when\u5b9e\u73b0\u6279\u91cf\u66f4\u65b0<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">update \u8868\u540d set \u5b57\u6bb5\u540d = case \u5b57\u6bb5\u540d when \u503c1 then '\u503c' when \u503c2 then '\u503c' when \u503c3 then '\u503c' end where s_id in (\u503c1,\u503c2,\u503c3);<\/pre>\n\n\n\n<p>\u4e09. \u5220\u9664\u6570\u636e\uff1aDELETE<\/p>\n\n\n\n<p>\u00a01. \u5220\u9664\u6307\u5b9a\u6570\u636e<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">DELETE FROM \u8868\u540d WHERE \u5217\u540d=\u503c;<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fd0000\" class=\"has-inline-color\">\u6ce8\u610f<\/mark>\uff1a\u5220\u9664\u7684\u65f6\u5019\u5982\u679c\u4e0d\u6307\u5b9awhere\u6761\u4ef6\uff0c\u5219\u4fdd\u7559\u6570\u636e\u8868\u7ed3\u6784\uff0c\u5220\u9664\u5168\u90e8\u6570\u636e\u884c\uff0c\u6709\u4e3b\u5916\u952e\u5173\u7cfb\u7684\u90fd\u5220\u4e0d\u4e86,\u4e0e SELECT \u8bed\u53e5\u4e0d\u540c\u7684\u662f\uff0cDELETE \u8bed\u53e5\u4e2d\u4e0d\u80fd\u4f7f\u7528 GROUP BY\u3001HAVING \u548c ORDER BY \u4e09\u7c7b\u5b50\u53e5\uff0c\u800c\u53ea\u80fd\u4f7f\u7528WHERE \u5b50\u53e5\u3002<\/p>\n\n\n\n<p>\u539f\u56e0\u5f88\u7b80\u5355\uff0c GROUP BY \u548c HAVING \u662f\u4ece\u8868\u4e2d\u9009\u53d6\u6570\u636e\u65f6\u7528\u6765\u6539\u53d8\u62bd\u53d6\u6570\u636e\u5f62\u5f0f\u7684\uff0c\u800c ORDER BY \u662f\u7528\u6765\u6307\u5b9a\u53d6\u5f97\u7ed3\u679c\u663e\u793a\u987a\u5e8f\u7684\u3002\u56e0\u6b64\uff0c\u5728\u5220\u9664\u8868\u4e2d\u6570\u636e\u65f6\u5b83\u4eec\u90fd\u8d77\u4e0d\u5230\u4ec0\u4e48\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>2. \u5220\u9664\u8868\u4e2d\u5168\u90e8\u6570\u636e<\/p>\n\n\n\n<pre style=\"color: #0099cc;background: #ffffcc\">TRUNCATE \u8868\u540d;<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\u6ce8\u610f<\/mark>\uff1a\u5168\u90e8\u5220\u9664\uff0c\u5185\u5b58\u65e0\u75d5\u8ff9\uff0c\u5982\u679c\u6709\u81ea\u589e\u4f1a\u91cd\u65b0\u5f00\u59cb\u7f16\u53f7\u3002<\/p>\n\n\n\n<p>\u4e0e DELETE \u4e0d\u540c\u7684\u662f\uff0cTRUNCATE \u53ea\u80fd\u5220\u9664\u8868\u4e2d\u7684\u5168\u90e8\u6570\u636e\uff0c\u800c\u4e0d\u80fd\u901a\u8fc7 WHERE \u5b50\u53e5\u6307\u5b9a\u6761\u4ef6\u6765\u5220\u9664\u90e8\u5206\u6570\u636e\u3002\u4e5f\u6b63\u662f\u56e0\u4e3a\u5b83\u4e0d\u80fd\u5177\u4f53\u5730\u63a7\u5236\u5220\u9664\u5bf9\u8c61\uff0c \u6240\u4ee5\u5176\u5904\u7406\u901f\u5ea6\u6bd4DELETE \u8981\u5feb\u5f97\u591a\u3002\u5b9e\u9645\u4e0a\uff0cDELETE\u8bed\u53e5\u5728DML\u8bed\u53e5\u4e2d\u4e5f\u5c5e\u4e8e\u5904\u7406\u65f6\u95f4\u6bd4\u8f83\u957f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u5220\u9664\u5168\u90e8\u6570\u636e\u884c\u65f6\uff0c\u4f7f\u7528 TRUNCATE \u53ef\u4ee5\u7f29\u77ed \u6267\u884c\u65f6\u95f4\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DML:\u6570\u636e\u64cd\u7eb5\u8bed\u53e5\uff0c\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u5e93\u5bf9\u8c61\u4e2d\u6240\u5305\u542b\u7684 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":0,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"wp:attachment":[{"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiwenbo.top\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}