{"id":168289,"date":"2024-11-06T11:08:25","date_gmt":"2024-11-06T03:08:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168289\/"},"modified":"2024-11-06T11:08:25","modified_gmt":"2024-11-06T03:08:25","slug":"%e6%af%8f%e5%80%8b%e9%96%8b%e7%99%bc%e4%ba%ba%e5%93%a1%e9%83%bd%e9%9c%80%e8%a6%81%e6%8e%8c%e6%8f%a1%e7%9a%84%e5%8d%81%e5%80%8b%e5%9f%ba%e6%9c%ac-sql-%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168289\/","title":{"rendered":"\u6bcf\u500b\u958b\u767c\u4eba\u54e1\u90fd\u9700\u8981\u638c\u63e1\u7684\u5341\u500b\u57fa\u672c SQL \u547d\u4ee4"},"content":{"rendered":"<h1 id=\"%e6%af%8f%e5%80%8b%e9%96%8b%e7%99%bc%e4%ba%ba%e5%93%a1%e9%83%bd%e9%9c%80%e8%a6%81%e6%8e%8c%e6%8f%a1%e7%9a%84%e5%8d%81%e5%80%8b%e5%9f%ba%e6%9c%ac-sql-%e5%91%bd%e4%bb%a4-TGXWquajuU\">\u6bcf\u500b\u958b\u767c\u4eba\u54e1\u90fd\u9700\u8981\u638c\u63e1\u7684\u5341\u500b\u57fa\u672c SQL \u547d\u4ee4<\/h1>\n<p>SQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u7528\u65bc\u7ba1\u7406\u548c\u64cd\u4f5c\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u6a19\u6e96\u8a9e\u8a00\u3002\u5c0d\u65bc\u6bcf\u4f4d\u958b\u767c\u4eba\u54e1\u4f86\u8aaa\uff0c\u638c\u63e1\u57fa\u672c\u7684 SQL \u547d\u4ee4\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5341\u500b\u57fa\u672c\u7684 SQL \u547d\u4ee4\uff0c\u5e6b\u52a9\u958b\u767c\u4eba\u54e1\u66f4\u6709\u6548\u5730\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"1-select-TGXWquajuU\">1. SELECT<\/h2>\n<p>SELECT \u547d\u4ee4\u7528\u65bc\u5f9e\u6578\u64da\u5eab\u4e2d\u67e5\u8a62\u6578\u64da\u3002\u9019\u662f\u6700\u5e38\u7528\u7684 SQL \u547d\u4ee4\u4e4b\u4e00\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u5f9e\u540d\u70ba &#8220;employees&#8221; \u7684\u8868\u4e2d\u9078\u64c7 &#8220;name&#8221; \u548c &#8220;age&#8221; \u5169\u500b\u6b04\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>SELECT name, age FROM employees;<\/code><\/pre>\n<h2 id=\"2-insert-TGXWquajuU\">2. INSERT<\/h2>\n<p>INSERT \u547d\u4ee4\u7528\u65bc\u5411\u6578\u64da\u5eab\u4e2d\u63d2\u5165\u65b0\u6578\u64da\u3002<\/p>\n<pre><code>INSERT INTO table_name (column1, column2) VALUES (value1, value2);<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5411 &#8220;employees&#8221; \u8868\u4e2d\u63d2\u5165\u4e00\u540d\u65b0\u54e1\u5de5\u7684\u8cc7\u6599\uff1a<\/p>\n<pre><code>INSERT INTO employees (name, age) VALUES ('John Doe', 30);<\/code><\/pre>\n<h2 id=\"3-update-TGXWquajuU\">3. UPDATE<\/h2>\n<p>UPDATE \u547d\u4ee4\u7528\u65bc\u66f4\u65b0\u6578\u64da\u5eab\u4e2d\u5df2\u5b58\u5728\u7684\u6578\u64da\u3002<\/p>\n<pre><code>UPDATE table_name SET column1 = value1 WHERE condition;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5c07 &#8220;employees&#8221; \u8868\u4e2d\u540d\u70ba &#8220;John Doe&#8221; \u7684\u54e1\u5de5\u5e74\u9f61\u66f4\u65b0\u70ba 31\uff1a<\/p>\n<pre><code>UPDATE employees SET age = 31 WHERE name = 'John Doe';<\/code><\/pre>\n<h2 id=\"4-delete-TGXWquajuU\">4. DELETE<\/h2>\n<p>DELETE \u547d\u4ee4\u7528\u65bc\u522a\u9664\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u3002<\/p>\n<pre><code>DELETE FROM table_name WHERE condition;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u522a\u9664 &#8220;employees&#8221; \u8868\u4e2d\u540d\u70ba &#8220;John Doe&#8221; \u7684\u54e1\u5de5\u8cc7\u6599\uff1a<\/p>\n<pre><code>DELETE FROM employees WHERE name = 'John Doe';<\/code><\/pre>\n<h2 id=\"5-create-table-TGXWquajuU\">5. CREATE TABLE<\/h2>\n<p>CREATE TABLE \u547d\u4ee4\u7528\u65bc\u5275\u5efa\u65b0\u7684\u6578\u64da\u8868\u3002<\/p>\n<pre><code>CREATE TABLE table_name (column1 datatype, column2 datatype, ...);<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5275\u5efa\u4e00\u500b\u540d\u70ba &#8220;employees&#8221; \u7684\u8868\uff1a<\/p>\n<pre><code>CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(100), age INT);<\/code><\/pre>\n<h2 id=\"6-alter-table-TGXWquajuU\">6. ALTER TABLE<\/h2>\n<p>ALTER TABLE \u547d\u4ee4\u7528\u65bc\u4fee\u6539\u5df2\u5b58\u5728\u7684\u6578\u64da\u8868\u7d50\u69cb\u3002<\/p>\n<pre><code>ALTER TABLE table_name ADD column_name datatype;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5411 &#8220;employees&#8221; \u8868\u4e2d\u6dfb\u52a0\u4e00\u500b &#8220;salary&#8221; \u6b04\u4f4d\uff1a<\/p>\n<pre><code>ALTER TABLE employees ADD salary DECIMAL(10, 2);<\/code><\/pre>\n<h2 id=\"7-drop-table-TGXWquajuU\">7. DROP TABLE<\/h2>\n<p>DROP TABLE \u547d\u4ee4\u7528\u65bc\u522a\u9664\u6574\u500b\u6578\u64da\u8868\u53ca\u5176\u6240\u6709\u6578\u64da\u3002<\/p>\n<pre><code>DROP TABLE table_name;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u522a\u9664 &#8220;employees&#8221; \u8868\uff1a<\/p>\n<pre><code>DROP TABLE employees;<\/code><\/pre>\n<h2 id=\"8-where-TGXWquajuU\">8. WHERE<\/h2>\n<p>WHERE \u5b50\u53e5\u7528\u65bc\u6307\u5b9a\u67e5\u8a62\u7684\u689d\u4ef6\uff0c\u904e\u6ffe\u7d50\u679c\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name WHERE condition;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u67e5\u8a62\u5e74\u9f61\u5927\u65bc 30 \u7684\u54e1\u5de5\uff1a<\/p>\n<pre><code>SELECT * FROM employees WHERE age &gt; 30;<\/code><\/pre>\n<h2 id=\"9-order-by-TGXWquajuU\">9. ORDER BY<\/h2>\n<p>ORDER BY \u5b50\u53e5\u7528\u65bc\u5c0d\u67e5\u8a62\u7d50\u679c\u9032\u884c\u6392\u5e8f\u3002<\/p>\n<pre><code>SELECT column1, column2 FROM table_name ORDER BY column1 ASC|DESC;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u6309\u5e74\u9f61\u5347\u5e8f\u6392\u5217\u54e1\u5de5\uff1a<\/p>\n<pre><code>SELECT * FROM employees ORDER BY age ASC;<\/code><\/pre>\n<h2 id=\"10-join-TGXWquajuU\">10. JOIN<\/h2>\n<p>JOIN \u547d\u4ee4\u7528\u65bc\u5c07\u4f86\u81ea\u5169\u500b\u6216\u591a\u500b\u8868\u7684\u6578\u64da\u7d50\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<pre><code>SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5f9e &#8220;employees&#8221; \u8868\u548c &#8220;departments&#8221; \u8868\u4e2d\u67e5\u8a62\u54e1\u5de5\u53ca\u5176\u6240\u5c6c\u90e8\u9580\uff1a<\/p>\n<pre><code>SELECT employees.name, departments.department_name \nFROM employees \nJOIN departments ON employees.department_id = departments.id;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-TGXWquajuU\">\u7e3d\u7d50<\/h2>\n<p>\u638c\u63e1\u9019\u5341\u500b\u57fa\u672c\u7684 SQL \u547d\u4ee4\u5c0d\u65bc\u6bcf\u4f4d\u958b\u767c\u4eba\u54e1\u4f86\u8aaa\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u9019\u4e9b\u547d\u4ee4\u4e0d\u50c5\u80fd\u5e6b\u52a9\u958b\u767c\u4eba\u54e1\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\uff0c\u9084\u80fd\u63d0\u9ad8\u6578\u64da\u64cd\u4f5c\u7684\u6548\u7387\u3002\u96a8\u8457\u6280\u8853\u7684\u767c\u5c55\uff0c\u5c0d\u65bc\u6578\u64da\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\uff0c\u719f\u7df4\u638c\u63e1 SQL \u5c07\u4f7f\u958b\u767c\u4eba\u54e1\u5728\u8077\u696d\u751f\u6daf\u4e2d\u66f4\u5177\u7af6\u722d\u529b\u3002\u5982\u679c\u60a8\u9700\u8981\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\uff0c\u8b93\u60a8\u7684\u9805\u76ee\u904b\u884c\u5f97\u66f4\u9806\u5229\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1\u9019\u5341\u500b\u57fa\u672c SQL \u547d\u4ee4\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u8b93\u6bcf\u4f4d\u958b\u767c\u4eba\u54e1\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u6e38\u5203\u6709\u9918\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-168289","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=168289"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168289\/revisions"}],"predecessor-version":[{"id":168290,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168289\/revisions\/168290"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}