{"id":78,"date":"2025-02-16T21:38:49","date_gmt":"2025-02-16T13:38:49","guid":{"rendered":"https:\/\/luwuge.cn\/?p=78"},"modified":"2025-02-16T21:38:49","modified_gmt":"2025-02-16T13:38:49","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e5%b8%b8%e7%94%a8%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/luwuge.cn\/index.php\/2025\/02\/16\/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e5%b8%b8%e7%94%a8%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95\/","title":{"rendered":"Mysql\u6570\u636e\u5e93\u5e38\u7528\u57fa\u7840\u8bed\u6cd5"},"content":{"rendered":"\n<p>MySQL \u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002\u5e38\u7528\u7684 SQL \u57fa\u7840\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p><strong>1. \u67e5\u8be2\u6570\u636e\uff08SELECT\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u67e5\u8be2\u6574\u4e2a\u8868\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;\u67e5\u8be2\u7279\u5b9a\u5217\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2 FROM table_name;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;\u4f7f\u7528\u6761\u4ef6\u67e5\u8be2\uff08WHERE\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name WHERE column_name = 'value';<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name WHERE column_name &gt; 10 AND column_name &lt; 50;<\/code><\/pre>\n\n\n\n<p><strong>2. \u6392\u5e8f\u6570\u636e\uff08ORDER BY\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u6309\u7167\u67d0\u5217\u5347\u5e8f\uff08\u9ed8\u8ba4\uff09\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name ORDER BY column_name;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;\u6309\u7167\u67d0\u5217\u964d\u5e8f\u6392\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name ORDER BY column_name DESC;<\/code><\/pre>\n\n\n\n<p><strong>3. \u9650\u5236\u67e5\u8be2\u7ed3\u679c\uff08LIMIT\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name LIMIT 10;  -- \u8fd4\u56de\u524d 10 \u884c\u6570\u636e<\/code><\/pre>\n\n\n\n<p><strong>4. \u63d2\u5165\u6570\u636e\uff08INSERT\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO table_name (column1, column2, ...) \nVALUES (value1, value2, ...);<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;\u63d2\u5165\u591a\u6761\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO table_name (column1, column2, ...) \nVALUES (value1, value2, ...), (value1, value2, ...);<\/code><\/pre>\n\n\n\n<p><strong>5. \u66f4\u65b0\u6570\u636e\uff08UPDATE\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u66f4\u65b0\u8868\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE table_name \nSET column1 = value1, column2 = value2 \nWHERE condition;<\/code><\/pre>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u5982\u679c\u4e0d\u52a0 WHERE \u6761\u4ef6\uff0c\u6240\u6709\u8bb0\u5f55\u90fd\u4f1a\u88ab\u66f4\u65b0\u3002<\/p>\n\n\n\n<p><strong>6. \u5220\u9664\u6570\u636e\uff08DELETE\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DELETE FROM table_name WHERE condition;<\/code><\/pre>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u540c\u6837\uff0c\u5982\u679c\u6ca1\u6709 WHERE \u6761\u4ef6\uff0c\u6240\u6709\u6570\u636e\u5c06\u88ab\u5220\u9664\u3002<\/p>\n\n\n\n<p><strong>7. \u521b\u5efa\u8868\uff08CREATE TABLE\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u521b\u5efa\u65b0\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE table_name (\n    column1 datatype,\n    column2 datatype,\n    ...\n);<\/code><\/pre>\n\n\n\n<p>\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u6709\uff1a<\/p>\n\n\n\n<p>\u2022&nbsp;INT\uff1a\u6574\u6570<\/p>\n\n\n\n<p>\u2022&nbsp;VARCHAR(size)\uff1a\u53d8\u957f\u5b57\u7b26\u4e32\uff0csize&nbsp;\u4e3a\u6700\u5927\u5b57\u7b26\u957f\u5ea6<\/p>\n\n\n\n<p>\u2022&nbsp;DATE\uff1a\u65e5\u671f<\/p>\n\n\n\n<p>\u2022&nbsp;DATETIME\uff1a\u65e5\u671f\u548c\u65f6\u95f4<\/p>\n\n\n\n<p><strong>8. \u4fee\u6539\u8868\u7ed3\u6784\uff08ALTER TABLE\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u4fee\u6539\u8868\u7684\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name ADD column_name datatype;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;\u4fee\u6539\u5b57\u6bb5\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name MODIFY column_name new_datatype;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;\u5220\u9664\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name DROP column_name;<\/code><\/pre>\n\n\n\n<p><strong>9. \u5220\u9664\u8868\uff08DROP TABLE\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u5220\u9664\u6574\u4e2a\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP TABLE table_name;<\/code><\/pre>\n\n\n\n<p><strong>10. \u805a\u5408\u51fd\u6570<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;<strong>COUNT<\/strong>\uff1a\u7edf\u8ba1\u884c\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) FROM table_name;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;<strong>SUM<\/strong>\uff1a\u8ba1\u7b97\u5217\u7684\u603b\u548c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SUM(column_name) FROM table_name;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;<strong>AVG<\/strong>\uff1a\u8ba1\u7b97\u5217\u7684\u5e73\u5747\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT AVG(column_name) FROM table_name;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;<strong>MIN<\/strong>\uff1a\u83b7\u53d6\u5217\u4e2d\u7684\u6700\u5c0f\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MIN(column_name) FROM table_name;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;<strong>MAX<\/strong>\uff1a\u83b7\u53d6\u5217\u4e2d\u7684\u6700\u5927\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT MAX(column_name) FROM table_name;<\/code><\/pre>\n\n\n\n<p><strong>11. \u5206\u7ec4\u6570\u636e\uff08GROUP BY\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u6309\u67d0\u4e2a\u5217\u5206\u7ec4\u5e76\u5e94\u7528\u805a\u5408\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column_name, COUNT(*) \nFROM table_name \nGROUP BY column_name;<\/code><\/pre>\n\n\n\n<p><strong>12. \u8fde\u63a5\u67e5\u8be2\uff08JOIN\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;<strong>\u5185\u8fde\u63a5\uff08INNER JOIN\uff09<\/strong>\uff1a\u8fd4\u56de\u4e24\u4e2a\u8868\u4e2d\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table1 \nINNER JOIN table2 \nON table1.column = table2.column;<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;<strong>\u5de6\u8fde\u63a5\uff08LEFT JOIN\uff09<\/strong>\uff1a\u8fd4\u56de\u5de6\u8868\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5373\u4f7f\u53f3\u8868\u6ca1\u6709\u5339\u914d\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table1 \nLEFT JOIN table2 \nON table1.column = table2.column;<\/code><\/pre>\n\n\n\n<p><strong>13. \u5b50\u67e5\u8be2\uff08Subquery\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u5b50\u67e5\u8be2\u5728\u4e3b\u67e5\u8be2\u4e2d\u4f5c\u4e3a\u6761\u4ef6\u6216\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM table_name \nWHERE column_name IN (SELECT column_name FROM another_table WHERE condition);<\/code><\/pre>\n\n\n\n<p><strong>14. \u7d22\u5f15\uff08INDEX\uff09<\/strong><\/p>\n\n\n\n<p>\u2022&nbsp;\u521b\u5efa\u7d22\u5f15\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE INDEX index_name ON table_name (column_name);<\/code><\/pre>\n\n\n\n<p>\u2022&nbsp;\u5220\u9664\u7d22\u5f15\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP INDEX index_name ON table_name;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e9b\u662f MySQL \u6570\u636e\u5e93\u4e2d\u5e38\u89c1\u7684\u4e00\u4e9b\u57fa\u7840\u64cd\u4f5c\u548c\u8bed\u6cd5\uff0c\u638c\u63e1\u8fd9\u4e9b\u8bed\u6cd5\u4e4b\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u5927\u591a\u6570\u5e38\u89c1\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u3002\u5e38\u7528\u7684 SQL \u57fa\u7840\u8bed\u6cd5\u5982\u4e0b\uff1a 1. \u67e5\u8be2\u6570\u636e\uff08SELECT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[14],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-12","tag-14"],"_links":{"self":[{"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":0,"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/luwuge.cn\/index.php\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}