{"id":14941,"date":"2024-08-25T03:56:52","date_gmt":"2024-08-24T19:56:52","guid":{"rendered":"https:\/\/fgchen.com\/wpedu\/?p=14941"},"modified":"2026-03-30T14:29:49","modified_gmt":"2026-03-30T06:29:49","slug":"sql%e9%80%9f%e8%a8%98%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/fgchen.com\/wpedu\/2024\/08\/sql%e9%80%9f%e8%a8%98%e8%a1%a8\/","title":{"rendered":"SQL\u901f\u8a18\u8868"},"content":{"rendered":"<p>\u57fa\u672c\u7684 SQL\u5e38\u898b\u7684\u67e5\u8a62\u548c\u64cd\u4f5c\u8a9e\u6cd5\uff1a<\/p>\n<h3>\u57fa\u672c SQL \u8a9e\u6cd5<\/h3>\n<h4>1. <strong>\u9078\u64c7\u6578\u64da\uff08SELECT\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1, column2, ...\nFROM table_name\nWHERE condition;<\/code><\/pre>\n<ul>\n<li><code>SELECT * FROM table_name;<\/code> &#8211; \u9078\u64c7\u8868\u4e2d\u7684\u6240\u6709\u6b04\u4f4d<\/li>\n<li><code>WHERE<\/code> &#8211; \u7528\u65bc\u904e\u6ffe\u689d\u4ef6<\/li>\n<li><code>DISTINCT<\/code> &#8211; \u904e\u6ffe\u91cd\u8907\u6578\u64da\uff1a<code>SELECT DISTINCT column1 FROM table_name;<\/code><\/li>\n<\/ul>\n<h4>2. <strong>\u63d2\u5165\u6578\u64da\uff08INSERT INTO\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">INSERT INTO table_name (column1, column2, ...)\nVALUES (value1, value2, ...);<\/code><\/pre>\n<ul>\n<li>\u63d2\u5165\u591a\u884c\uff1a<code>INSERT INTO table_name (column1, column2, ...) VALUES (value1a, value2a), (value1b, value2b);<\/code><\/li>\n<\/ul>\n<h4>3. <strong>\u66f4\u65b0\u6578\u64da\uff08UPDATE\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">UPDATE table_name\nSET column1 = value1, column2 = value2, ...\nWHERE condition;<\/code><\/pre>\n<ul>\n<li>\u8acb\u6ce8\u610f\uff1a\u82e5\u4e0d\u52a0 <code>WHERE<\/code> \u689d\u4ef6\uff0c\u5c07\u6703\u66f4\u65b0\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/li>\n<\/ul>\n<h4>4. <strong>\u522a\u9664\u6578\u64da\uff08DELETE\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">DELETE FROM table_name\nWHERE condition;<\/code><\/pre>\n<ul>\n<li>\u82e5\u4e0d\u52a0 <code>WHERE<\/code> \u689d\u4ef6\uff0c\u5c07\u6703\u522a\u9664\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/li>\n<\/ul>\n<h3>\u9ad8\u7d1a SQL \u67e5\u8a62<\/h3>\n<h4>1. <strong>\u689d\u4ef6\u67e5\u8a62\uff08WHERE\u3001AND\u3001OR\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1, column2\nFROM table_name\nWHERE condition1 AND condition2\nOR condition3;<\/code><\/pre>\n<h4>2. <strong>\u7bc4\u570d\u67e5\u8a62\uff08BETWEEN\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1, column2\nFROM table_name\nWHERE column1 BETWEEN value1 AND value2;<\/code><\/pre>\n<h4>3. <strong>\u6a21\u7cca\u67e5\u8a62\uff08LIKE\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1, column2\nFROM table_name\nWHERE column1 LIKE &#039;pattern%&#039;;<\/code><\/pre>\n<ul>\n<li><code>%<\/code> \u8868\u793a\u4efb\u610f\u6578\u91cf\u5b57\u7b26\uff0c<code>_<\/code> \u8868\u793a\u55ae\u500b\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<h4>4. <strong>\u7a7a\u503c\u6aa2\u67e5\uff08IS NULL \/ IS NOT NULL\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1, column2\nFROM table_name\nWHERE column1 IS NULL;<\/code><\/pre>\n<h4>5. <strong>\u6392\u5e8f\uff08ORDER BY\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1, column2\nFROM table_name\nORDER BY column1 ASC, column2 DESC;<\/code><\/pre>\n<ul>\n<li><code>ASC<\/code> &#8211; \u5347\u5e8f\uff08\u9ed8\u8a8d\uff09\uff0c<code>DESC<\/code> &#8211; \u964d\u5e8f<\/li>\n<\/ul>\n<h4>6. <strong>\u5206\u7d44\uff08GROUP BY \u548c HAVING\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1, COUNT(*)\nFROM table_name\nGROUP BY column1\nHAVING COUNT(*) &gt; 1;<\/code><\/pre>\n<h4>7. <strong>\u805a\u5408\u51fd\u6578<\/strong><\/h4>\n<ul>\n<li><code>COUNT(column)<\/code> &#8211; \u8a08\u6578<\/li>\n<li><code>SUM(column)<\/code> &#8211; \u7e3d\u548c<\/li>\n<li><code>AVG(column)<\/code> &#8211; \u5e73\u5747<\/li>\n<li><code>MAX(column)<\/code> &#8211; \u6700\u5927\u503c<\/li>\n<li><code>MIN(column)<\/code> &#8211; \u6700\u5c0f\u503c<\/li>\n<\/ul>\n<h3>\u8868\u64cd\u4f5c<\/h3>\n<h4>1. <strong>\u5275\u5efa\u8868\uff08CREATE TABLE\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">CREATE TABLE table_name (\n    column1 datatype PRIMARY KEY,\n    column2 datatype NOT NULL,\n    column3 datatype DEFAULT value\n);<\/code><\/pre>\n<h4>2. <strong>\u4fee\u6539\u8868\u7d50\u69cb\uff08ALTER TABLE\uff09<\/strong><\/h4>\n<ul>\n<li>\u6dfb\u52a0\u6b04\u4f4d\uff1a<code>ALTER TABLE table_name ADD column_name datatype;<\/code><\/li>\n<li>\u4fee\u6539\u6b04\u4f4d\u985e\u578b\uff1a<code>ALTER TABLE table_name MODIFY column_name new_datatype;<\/code><\/li>\n<li>\u522a\u9664\u6b04\u4f4d\uff1a<code>ALTER TABLE table_name DROP COLUMN column_name;<\/code><\/li>\n<\/ul>\n<h4>3. <strong>\u522a\u9664\u8868\uff08DROP TABLE\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">DROP TABLE table_name;<\/code><\/pre>\n<h4>4. <strong>\u6e05\u7a7a\u8868\uff08TRUNCATE TABLE\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">TRUNCATE TABLE table_name;<\/code><\/pre>\n<h3>\u93c8\u7d50\uff08JOIN\uff09<\/h3>\n<h4>1. <strong>\u5167\u806f\u63a5\uff08INNER JOIN\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT table1.column1, table2.column2\nFROM table1\nINNER JOIN table2 ON table1.common_column = table2.common_column;<\/code><\/pre>\n<h4>2. <strong>\u5de6\u806f\u63a5\uff08LEFT JOIN\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT table1.column1, table2.column2\nFROM table1\nLEFT JOIN table2 ON table1.common_column = table2.common_column;<\/code><\/pre>\n<h4>3. <strong>\u53f3\u806f\u63a5\uff08RIGHT JOIN\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT table1.column1, table2.column2\nFROM table1\nRIGHT JOIN table2 ON table1.common_column = table2.common_column;<\/code><\/pre>\n<h4>4. <strong>\u5168\u806f\u63a5\uff08FULL OUTER JOIN\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT table1.column1, table2.column2\nFROM table1\nFULL OUTER JOIN table2 ON table1.common_column = table2.common_column;<\/code><\/pre>\n<h3>\u5b50\u67e5\u8a62\u548c\u8907\u5408\u67e5\u8a62<\/h3>\n<h4>1. <strong>\u5b50\u67e5\u8a62\uff08Subquery\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">SELECT column1\nFROM table_name\nWHERE column2 = (SELECT column2 FROM another_table WHERE condition);<\/code><\/pre>\n<h4>2. <strong>UNION \u548c UNION ALL<\/strong><\/h4>\n<ul>\n<li><code>UNION<\/code> &#8211; \u5408\u4f75\u7d50\u679c\u96c6\uff0c\u53bb\u91cd<\/li>\n<li><code>UNION ALL<\/code> &#8211; \u5408\u4f75\u7d50\u679c\u96c6\uff0c\u4e0d\u53bb\u91cd<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">SELECT column1 FROM table1\nUNION\nSELECT column1 FROM table2;<\/code><\/pre>\n<h3>\u7d22\u5f15\u548c\u6027\u80fd\u512a\u5316<\/h3>\n<h4>1. <strong>\u5275\u5efa\u7d22\u5f15\uff08CREATE INDEX\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">CREATE INDEX index_name ON table_name (column1, column2);<\/code><\/pre>\n<ul>\n<li>\u552f\u4e00\u7d22\u5f15\uff1a<code>CREATE UNIQUE INDEX index_name ON table_name (column);<\/code><\/li>\n<\/ul>\n<h4>2. <strong>\u522a\u9664\u7d22\u5f15\uff08DROP INDEX\uff09<\/strong><\/h4>\n<pre><code class=\"language-sql\">DROP INDEX index_name ON table_name;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u7684 SQL\u5e38\u898b\u7684\u67e5\u8a62\u548c\u64cd\u4f5c\u8a9e\u6cd5\uff1a \u57fa\u672c SQL \u8a9e\u6cd5 1. \u9078\u64c7\u6578\u64da\uff08SEL &hellip; <\/p>\n","protected":false},"author":1,"featured_media":14942,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[266],"tags":[],"class_list":["post-14941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-266"],"_links":{"self":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/14941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/comments?post=14941"}],"version-history":[{"count":2,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/14941\/revisions"}],"predecessor-version":[{"id":14944,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/14941\/revisions\/14944"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media\/14942"}],"wp:attachment":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media?parent=14941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/categories?post=14941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/tags?post=14941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}