{"id":275,"date":"2018-11-21T20:51:38","date_gmt":"2018-11-21T12:51:38","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=275"},"modified":"2019-02-21T10:11:32","modified_gmt":"2019-02-21T02:11:32","slug":"sql%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=275","title":{"rendered":"SQL\u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/github.com\/CyC2018\/\" target=\"_blank\" rel=\"noopener\">\u53c2\u8003<\/a><\/li>\n<\/ul>\n<h1>\u4e00\u3001\u57fa\u7840<\/h1>\n<p>\u6a21\u5f0f\u5b9a\u4e49\u4e86\u6570\u636e\u5982\u4f55\u5b58\u50a8\u3001\u5b58\u50a8\u4ec0\u4e48\u6837\u7684\u6570\u636e\u4ee5\u53ca\u6570\u636e\u5982\u4f55\u5206\u89e3\u7b49\u4fe1\u606f\uff0c\u6570\u636e\u5e93\u548c\u8868\u90fd\u6709\u6a21\u5f0f\u3002<\/p>\n<p>\u4e3b\u952e\u7684\u503c\u4e0d\u5141\u8bb8\u4fee\u6539\uff0c\u4e5f\u4e0d\u5141\u8bb8\u590d\u7528\uff08\u4e0d\u80fd\u4f7f\u7528\u5df2\u7ecf\u5220\u9664\u7684\u4e3b\u952e\u503c\u8d4b\u7ed9\u65b0\u6570\u636e\u884c\u7684\u4e3b\u952e\uff09\u3002<\/p>\n<p>SQL\uff08Structured Query Language)\uff0c\u6807\u51c6 SQL \u7531 ANSI \u6807\u51c6\u59d4\u5458\u4f1a\u7ba1\u7406\uff0c\u4ece\u800c\u79f0\u4e3a ANSI SQL\u3002\u5404\u4e2a DBMS \u90fd\u6709\u81ea\u5df1\u7684\u5b9e\u73b0\uff0c\u5982 PL\/SQL\u3001Transact-SQL \u7b49\u3002<\/p>\n<p>SQL \u8bed\u53e5\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u4f46\u662f\u6570\u636e\u5e93\u8868\u540d\u3001\u5217\u540d\u548c\u503c\u662f\u5426\u533a\u5206\u4f9d\u8d56\u4e8e\u5177\u4f53\u7684 DBMS \u4ee5\u53ca\u914d\u7f6e\u3002<\/p>\n<p>SQL \u652f\u6301\u4ee5\u4e0b\u4e09\u79cd\u6ce8\u91ca\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\n# \u6ce8\u91ca\nSELECT *\nFROM mytable; -- \u6ce8\u91ca\n\/* \u6ce8\u91ca1\n   \u6ce8\u91ca2 *\/\n<\/pre>\n<p>\u6570\u636e\u5e93\u521b\u5efa\u4e0e\u4f7f\u7528\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE DATABASE test;\nUSE test;\n<\/pre>\n<h1>\u4e8c\u3001\u521b\u5efa\u8868<\/h1>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE TABLE mytable (\n  id INT NOT NULL AUTO_INCREMENT,\n  col1 INT NOT NULL DEFAULT 1,\n  col2 VARCHAR(45) NULL,\n  col3 DATE NULL,\n  PRIMARY KEY (`id`));\n<\/pre>\n<h1>\u4e09\u3001\u4fee\u6539\u8868<\/h1>\n<p>\u6dfb\u52a0\u5217<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nALTER TABLE mytable\nADD col CHAR(20);\n<\/pre>\n<p>\u5220\u9664\u5217<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nALTER TABLE mytable\nDROP COLUMN col;\n<\/pre>\n<p>\u5220\u9664\u8868<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nDROP TABLE mytable;\n<\/pre>\n<h1>\u56db\u3001\u63d2\u5165<\/h1>\n<p>\u666e\u901a\u63d2\u5165<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nINSERT INTO mytable(col1, col2)\nVALUES(val1, val2);\n<\/pre>\n<p>\u63d2\u5165\u68c0\u7d22\u51fa\u6765\u7684\u6570\u636e<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nINSERT INTO mytable1(col1, col2)\nSELECT col1, col2\nFROM mytable2;\n<\/pre>\n<p>\u5c06\u4e00\u4e2a\u8868\u7684\u5185\u5bb9\u63d2\u5165\u5230\u4e00\u4e2a\u65b0\u8868<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE TABLE newtable AS\nSELECT * FROM mytable;\n<\/pre>\n<h1>\u4e94\u3001\u66f4\u65b0<\/h1>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nUPDATE mytable\nSET col = val\nWHERE id = 1;\n<\/pre>\n<h1>\u516d\u3001\u5220\u9664<\/h1>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nDELETE FROM mytable\nWHERE id = 1;\n<\/pre>\n<p><strong>TRUNCATE TABLE<\/strong>  \u53ef\u4ee5\u6e05\u7a7a\u8868\uff0c\u4e5f\u5c31\u662f\u5220\u9664\u6240\u6709\u884c\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nTRUNCATE TABLE mytable;\n<\/pre>\n<p>\u4f7f\u7528\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u65f6\u4e00\u5b9a\u8981\u7528 WHERE \u5b50\u53e5\uff0c\u4e0d\u7136\u4f1a\u628a\u6574\u5f20\u8868\u7684\u6570\u636e\u90fd\u7834\u574f\u3002\u53ef\u4ee5\u5148\u7528 SELECT \u8bed\u53e5\u8fdb\u884c\u6d4b\u8bd5\uff0c\u9632\u6b62\u9519\u8bef\u5220\u9664\u3002<\/p>\n<h1>\u4e03\u3001\u67e5\u8be2<\/h1>\n<h2>DISTINCT<\/h2>\n<p>\u76f8\u540c\u503c\u53ea\u4f1a\u51fa\u73b0\u4e00\u6b21\u3002\u5b83\u4f5c\u7528\u4e8e\u6240\u6709\u5217\uff0c\u4e5f\u5c31\u662f\u8bf4\u6240\u6709\u5217\u7684\u503c\u90fd\u76f8\u540c\u624d\u7b97\u76f8\u540c\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT DISTINCT col1, col2\nFROM mytable;\n<\/pre>\n<h2>LIMIT<\/h2>\n<p>\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570\u3002\u53ef\u4ee5\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u8d77\u59cb\u884c\uff0c\u4ece 0 \u5f00\u59cb\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8fd4\u56de\u7684\u603b\u884c\u6570\u3002<\/p>\n<p>\u8fd4\u56de\u524d 5 \u884c\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nLIMIT 5;\n<\/pre>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nLIMIT 0, 5;\n<\/pre>\n<p>\u8fd4\u56de\u7b2c 3 \\~ 5 \u884c\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nLIMIT 2, 3;\n<\/pre>\n<h1>\u516b\u3001\u6392\u5e8f<\/h1>\n<ul>\n<li><strong>ASC<\/strong> \uff1a\u5347\u5e8f\uff08\u9ed8\u8ba4\uff09<\/li>\n<li><strong>DESC<\/strong> \uff1a\u964d\u5e8f<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u6309\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4e14\u4e3a\u6bcf\u4e2a\u5217\u6307\u5b9a\u4e0d\u540c\u7684\u6392\u5e8f\u65b9\u5f0f\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nORDER BY col1 DESC, col2 ASC;\n<\/pre>\n<h1>\u4e5d\u3001\u8fc7\u6ee4<\/h1>\n<p>\u4e0d\u8fdb\u884c\u8fc7\u6ee4\u7684\u6570\u636e\u975e\u5e38\u5927\uff0c\u5bfc\u81f4\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u4e86\u591a\u4f59\u7684\u6570\u636e\uff0c\u4ece\u800c\u6d6a\u8d39\u4e86\u7f51\u7edc\u5e26\u5bbd\u3002\u56e0\u6b64\u5c3d\u91cf\u4f7f\u7528 SQL \u8bed\u53e5\u6765\u8fc7\u6ee4\u4e0d\u5fc5\u8981\u7684\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4f20\u8f93\u6240\u6709\u7684\u6570\u636e\u5230\u5ba2\u6237\u7aef\u4e2d\u7136\u540e\u7531\u5ba2\u6237\u7aef\u8fdb\u884c\u8fc7\u6ee4\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nWHERE col IS NULL;\n<\/pre>\n<p>\u4e0b\u8868\u663e\u793a\u4e86 WHERE \u5b50\u53e5\u53ef\u7528\u7684\u64cd\u4f5c\u7b26<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">\u64cd\u4f5c\u7b26<\/th>\n<th align=\"center\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">=<\/td>\n<td align=\"center\">\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&lt;<\/td>\n<td align=\"center\">\u5c0f\u4e8e<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&gt;<\/td>\n<td align=\"center\">\u5927\u4e8e<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&lt;&gt; !=<\/td>\n<td align=\"center\">\u4e0d\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&lt;= !&gt;<\/td>\n<td align=\"center\">\u5c0f\u4e8e\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">&gt;= !&lt;<\/td>\n<td align=\"center\">\u5927\u4e8e\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">BETWEEN<\/td>\n<td align=\"center\">\u5728\u4e24\u4e2a\u503c\u4e4b\u95f4<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">IS NULL<\/td>\n<td align=\"center\">\u4e3a NULL \u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e94\u8be5\u6ce8\u610f\u5230\uff0cNULL \u4e0e 0\u3001\u7a7a\u5b57\u7b26\u4e32\u90fd\u4e0d\u540c\u3002<\/p>\n<p><strong>AND \u548c OR<\/strong>  \u7528\u4e8e\u8fde\u63a5\u591a\u4e2a\u8fc7\u6ee4\u6761\u4ef6\u3002\u4f18\u5148\u5904\u7406 AND\uff0c\u5f53\u4e00\u4e2a\u8fc7\u6ee4\u8868\u8fbe\u5f0f\u6d89\u53ca\u5230\u591a\u4e2a AND \u548c OR \u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 () \u6765\u51b3\u5b9a\u4f18\u5148\u7ea7\uff0c\u4f7f\u5f97\u4f18\u5148\u7ea7\u5173\u7cfb\u66f4\u6e05\u6670\u3002<\/p>\n<p><strong>IN<\/strong>  \u64cd\u4f5c\u7b26\u7528\u4e8e\u5339\u914d\u4e00\u7ec4\u503c\uff0c\u5176\u540e\u4e5f\u53ef\u4ee5\u63a5\u4e00\u4e2a SELECT \u5b50\u53e5\uff0c\u4ece\u800c\u5339\u914d\u5b50\u67e5\u8be2\u5f97\u5230\u7684\u4e00\u7ec4\u503c\u3002<\/p>\n<p><strong>NOT<\/strong>  \u64cd\u4f5c\u7b26\u7528\u4e8e\u5426\u5b9a\u4e00\u4e2a\u6761\u4ef6\u3002<\/p>\n<h1>\u5341\u3001\u901a\u914d\u7b26<\/h1>\n<p>\u901a\u914d\u7b26\u4e5f\u662f\u7528\u5728\u8fc7\u6ee4\u8bed\u53e5\u4e2d\uff0c\u4f46\u5b83\u53ea\u80fd\u7528\u4e8e\u6587\u672c\u5b57\u6bb5\u3002<\/p>\n<ul>\n<li><strong>%<\/strong>  \u5339\u914d >=0 \u4e2a\u4efb\u610f\u5b57\u7b26\uff1b<\/p>\n<\/li>\n<li>\n<p><strong>&#95;<\/strong>  \u5339\u914d ==1 \u4e2a\u4efb\u610f\u5b57\u7b26\uff1b<\/p>\n<\/li>\n<li>\n<p><strong><input style=\"margin-right:5px\" type=\"checkbox\" class=\"task-list-item-checkbox\" disabled \/><\/strong>  \u53ef\u4ee5\u5339\u914d\u96c6\u5408\u5185\u7684\u5b57\u7b26\uff0c\u4f8b\u5982 [ab] \u5c06\u5339\u914d\u5b57\u7b26 a \u6216\u8005 b\u3002\u7528\u8131\u5b57\u7b26 ^ \u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5426\u5b9a\uff0c\u4e5f\u5c31\u662f\u4e0d\u5339\u914d\u96c6\u5408\u5185\u7684\u5b57\u7b26\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4f7f\u7528 Like \u6765\u8fdb\u884c\u901a\u914d\u7b26\u5339\u914d\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nWHERE col LIKE &amp;#039;[^AB]%&amp;#039;; -- \u4e0d\u4ee5 A \u548c B \u5f00\u5934\u7684\u4efb\u610f\u6587\u672c\n<\/pre>\n<p>\u4e0d\u8981\u6ee5\u7528\u901a\u914d\u7b26\uff0c\u901a\u914d\u7b26\u4f4d\u4e8e\u5f00\u5934\u5904\u5339\u914d\u4f1a\u975e\u5e38\u6162\u3002<\/p>\n<h1>\u5341\u4e00\u3001\u8ba1\u7b97\u5b57\u6bb5<\/h1>\n<p>\u5728\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e0a\u5b8c\u6210\u6570\u636e\u7684\u8f6c\u6362\u548c\u683c\u5f0f\u5316\u7684\u5de5\u4f5c\u5f80\u5f80\u6bd4\u5ba2\u6237\u7aef\u4e0a\u5feb\u5f97\u591a\uff0c\u5e76\u4e14\u8f6c\u6362\u548c\u683c\u5f0f\u5316\u540e\u7684\u6570\u636e\u91cf\u66f4\u5c11\u7684\u8bdd\u53ef\u4ee5\u51cf\u5c11\u7f51\u7edc\u901a\u4fe1\u91cf\u3002<\/p>\n<p>\u8ba1\u7b97\u5b57\u6bb5\u901a\u5e38\u9700\u8981\u4f7f\u7528  <strong>AS<\/strong>  \u6765\u53d6\u522b\u540d\uff0c\u5426\u5219\u8f93\u51fa\u7684\u65f6\u5019\u5b57\u6bb5\u540d\u4e3a\u8ba1\u7b97\u8868\u8fbe\u5f0f\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT col1 * col2 AS alias\nFROM mytable;\n<\/pre>\n<p><strong>CONCAT()<\/strong>  \u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u5b57\u6bb5\u3002\u8bb8\u591a\u6570\u636e\u5e93\u4f1a\u4f7f\u7528\u7a7a\u683c\u628a\u4e00\u4e2a\u503c\u586b\u5145\u4e3a\u5217\u5bbd\uff0c\u56e0\u6b64\u8fde\u63a5\u7684\u7ed3\u679c\u4f1a\u51fa\u73b0\u4e00\u4e9b\u4e0d\u5fc5\u8981\u7684\u7a7a\u683c\uff0c\u4f7f\u7528 <strong>TRIM()<\/strong> \u53ef\u4ee5\u53bb\u9664\u9996\u5c3e\u7a7a\u683c\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT CONCAT(TRIM(col1), &amp;#039;(&amp;#039;, TRIM(col2), &amp;#039;)&amp;#039;) AS concat_col\nFROM mytable;\n<\/pre>\n<h1>\u5341\u4e8c\u3001\u51fd\u6570<\/h1>\n<p>\u5404\u4e2a DBMS \u7684\u51fd\u6570\u90fd\u662f\u4e0d\u76f8\u540c\u7684\uff0c\u56e0\u6b64\u4e0d\u53ef\u79fb\u690d\uff0c\u4ee5\u4e0b\u4e3b\u8981\u662f MySQL \u7684\u51fd\u6570\u3002<\/p>\n<h2>\u6c47\u603b<\/h2>\n<table>\n<thead>\n<tr>\n<th align=\"center\">\u51fd \u6570<\/th>\n<th align=\"center\">\u8bf4 \u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">AVG()<\/td>\n<td align=\"center\">\u8fd4\u56de\u67d0\u5217\u7684\u5e73\u5747\u503c<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">COUNT()<\/td>\n<td align=\"center\">\u8fd4\u56de\u67d0\u5217\u7684\u884c\u6570<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">MAX()<\/td>\n<td align=\"center\">\u8fd4\u56de\u67d0\u5217\u7684\u6700\u5927\u503c<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">MIN()<\/td>\n<td align=\"center\">\u8fd4\u56de\u67d0\u5217\u7684\u6700\u5c0f\u503c<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">SUM()<\/td>\n<td align=\"center\">\u8fd4\u56de\u67d0\u5217\u503c\u4e4b\u548c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>AVG() \u4f1a\u5ffd\u7565 NULL \u884c\u3002<\/p>\n<p>\u4f7f\u7528 DISTINCT \u53ef\u4ee5\u8ba9\u6c47\u603b\u51fd\u6570\u503c\u6c47\u603b\u4e0d\u540c\u7684\u503c\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT AVG(DISTINCT col1) AS avg_col\nFROM mytable;\n<\/pre>\n<h2>\u6587\u672c\u5904\u7406<\/h2>\n<table>\n<thead>\n<tr>\n<th align=\"center\">\u51fd\u6570<\/th>\n<th align=\"center\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">LEFT()<\/td>\n<td align=\"center\">\u5de6\u8fb9\u7684\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">RIGHT()<\/td>\n<td align=\"center\">\u53f3\u8fb9\u7684\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">LOWER()<\/td>\n<td align=\"center\">\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">UPPER()<\/td>\n<td align=\"center\">\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">LTRIM()<\/td>\n<td align=\"center\">\u53bb\u9664\u5de6\u8fb9\u7684\u7a7a\u683c<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">RTRIM()<\/td>\n<td align=\"center\">\u53bb\u9664\u53f3\u8fb9\u7684\u7a7a\u683c<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">LENGTH()<\/td>\n<td align=\"center\">\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">SOUNDEX()<\/td>\n<td align=\"center\">\u8f6c\u6362\u4e3a\u8bed\u97f3\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d\uff0c <strong>SOUNDEX()<\/strong>  \u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u63cf\u8ff0\u5176\u8bed\u97f3\u8868\u793a\u7684\u5b57\u6bcd\u6570\u5b57\u6a21\u5f0f\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nWHERE SOUNDEX(col1) = SOUNDEX(&amp;#039;apple&amp;#039;)\n<\/pre>\n<h2>\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406<\/h2>\n<ul>\n<li>\u65e5\u671f\u683c\u5f0f\uff1aYYYY-MM-DD<\/li>\n<li>\u65f6\u95f4\u683c\u5f0f\uff1aHH:MM:SS<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th align=\"center\">\u51fd \u6570<\/th>\n<th align=\"center\">\u8bf4 \u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">AddDate()<\/td>\n<td align=\"center\">\u589e\u52a0\u4e00\u4e2a\u65e5\u671f\uff08\u5929\u3001\u5468\u7b49\uff09<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">AddTime()<\/td>\n<td align=\"center\">\u589e\u52a0\u4e00\u4e2a\u65f6\u95f4\uff08\u65f6\u3001\u5206\u7b49\uff09<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">CurDate()<\/td>\n<td align=\"center\">\u8fd4\u56de\u5f53\u524d\u65e5\u671f<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">CurTime()<\/td>\n<td align=\"center\">\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Date()<\/td>\n<td align=\"center\">\u8fd4\u56de\u65e5\u671f\u65f6\u95f4\u7684\u65e5\u671f\u90e8\u5206<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">DateDiff()<\/td>\n<td align=\"center\">\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u5dee<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Date_Add()<\/td>\n<td align=\"center\">\u9ad8\u5ea6\u7075\u6d3b\u7684\u65e5\u671f\u8fd0\u7b97\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Date_Format()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u683c\u5f0f\u5316\u7684\u65e5\u671f\u6216\u65f6\u95f4\u4e32<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Day()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u65e5\u671f\u7684\u5929\u6570\u90e8\u5206<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">DayOfWeek()<\/td>\n<td align=\"center\">\u5bf9\u4e8e\u4e00\u4e2a\u65e5\u671f\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u661f\u671f\u51e0<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Hour()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u65f6\u95f4\u7684\u5c0f\u65f6\u90e8\u5206<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Minute()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u65f6\u95f4\u7684\u5206\u949f\u90e8\u5206<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Month()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u65e5\u671f\u7684\u6708\u4efd\u90e8\u5206<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Now()<\/td>\n<td align=\"center\">\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Second()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u65f6\u95f4\u7684\u79d2\u90e8\u5206<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Time()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u7684\u65f6\u95f4\u90e8\u5206<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">Year()<\/td>\n<td align=\"center\">\u8fd4\u56de\u4e00\u4e2a\u65e5\u671f\u7684\u5e74\u4efd\u90e8\u5206<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nmysql&amp;gt; SELECT NOW();\n<\/pre>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n2018-4-14 20:25:11\n<\/pre>\n<h2>\u6570\u503c\u5904\u7406<\/h2>\n<table>\n<thead>\n<tr>\n<th align=\"center\">\u51fd\u6570<\/th>\n<th align=\"center\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">SIN()<\/td>\n<td align=\"center\">\u6b63\u5f26<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">COS()<\/td>\n<td align=\"center\">\u4f59\u5f26<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">TAN()<\/td>\n<td align=\"center\">\u6b63\u5207<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">ABS()<\/td>\n<td align=\"center\">\u7edd\u5bf9\u503c<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">SQRT()<\/td>\n<td align=\"center\">\u5e73\u65b9\u6839<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">MOD()<\/td>\n<td align=\"center\">\u4f59\u6570<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">EXP()<\/td>\n<td align=\"center\">\u6307\u6570<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">PI()<\/td>\n<td align=\"center\">\u5706\u5468\u7387<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">RAND()<\/td>\n<td align=\"center\">\u968f\u673a\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>\u5341\u4e09\u3001\u5206\u7ec4<\/h1>\n<p>\u5206\u7ec4\u5c31\u662f\u628a\u5177\u6709\u76f8\u540c\u7684\u6570\u636e\u503c\u7684\u884c\u653e\u5728\u540c\u4e00\u7ec4\u4e2d\u3002<\/p>\n<p>\u53ef\u4ee5\u5bf9\u540c\u4e00\u5206\u7ec4\u6570\u636e\u4f7f\u7528\u6c47\u603b\u51fd\u6570\u8fdb\u884c\u5904\u7406\uff0c\u4f8b\u5982\u6c42\u5206\u7ec4\u6570\u636e\u7684\u5e73\u5747\u503c\u7b49\u3002<\/p>\n<p>\u6307\u5b9a\u7684\u5206\u7ec4\u5b57\u6bb5\u9664\u4e86\u80fd\u6309\u8be5\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4\uff0c\u4e5f\u4f1a\u81ea\u52a8\u6309\u8be5\u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT col, COUNT(*) AS num\nFROM mytable\nGROUP BY col;\n<\/pre>\n<p>GROUP BY \u81ea\u52a8\u6309\u5206\u7ec4\u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\uff0cORDER BY \u4e5f\u53ef\u4ee5\u6309\u6c47\u603b\u5b57\u6bb5\u6765\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT col, COUNT(*) AS num\nFROM mytable\nGROUP BY col\nORDER BY num;\n<\/pre>\n<p>WHERE \u8fc7\u6ee4\u884c\uff0cHAVING \u8fc7\u6ee4\u5206\u7ec4\uff0c\u884c\u8fc7\u6ee4\u5e94\u5f53\u5148\u4e8e\u5206\u7ec4\u8fc7\u6ee4\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT col, COUNT(*) AS num\nFROM mytable\nWHERE col &amp;gt; 2\nGROUP BY col\nHAVING num &amp;gt;= 2;\n<\/pre>\n<p>\u5206\u7ec4\u89c4\u5b9a\uff1a<\/p>\n<ul>\n<li>GROUP BY \u5b50\u53e5\u51fa\u73b0\u5728 WHERE \u5b50\u53e5\u4e4b\u540e\uff0cORDER BY \u5b50\u53e5\u4e4b\u524d\uff1b<\/li>\n<li>\u9664\u4e86\u6c47\u603b\u5b57\u6bb5\u5916\uff0cSELECT \u8bed\u53e5\u4e2d\u7684\u6bcf\u4e00\u5b57\u6bb5\u90fd\u5fc5\u987b\u5728 GROUP BY \u5b50\u53e5\u4e2d\u7ed9\u51fa\uff1b<\/li>\n<li>NULL \u7684\u884c\u4f1a\u5355\u72ec\u5206\u4e3a\u4e00\u7ec4\uff1b<\/li>\n<li>\u5927\u591a\u6570 SQL \u5b9e\u73b0\u4e0d\u652f\u6301 GROUP BY \u5217\u5177\u6709\u53ef\u53d8\u957f\u5ea6\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<h1>\u5341\u56db\u3001\u5b50\u67e5\u8be2<\/h1>\n<p>\u5b50\u67e5\u8be2\u4e2d\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u5b57\u6bb5\u7684\u6570\u636e\u3002<\/p>\n<p>\u53ef\u4ee5\u5c06\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a WHRER \u8bed\u53e5\u7684\u8fc7\u6ee4\u6761\u4ef6\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable1\nWHERE col1 IN (SELECT col2\n               FROM mytable2);\n<\/pre>\n<p>\u4e0b\u9762\u7684\u8bed\u53e5\u53ef\u4ee5\u68c0\u7d22\u51fa\u5ba2\u6237\u7684\u8ba2\u5355\u6570\u91cf\uff0c\u5b50\u67e5\u8be2\u8bed\u53e5\u4f1a\u5bf9\u7b2c\u4e00\u4e2a\u67e5\u8be2\u68c0\u7d22\u51fa\u7684\u6bcf\u4e2a\u5ba2\u6237\u6267\u884c\u4e00\u6b21\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT cust_name, (SELECT COUNT(*)\n                   FROM Orders\n                   WHERE Orders.cust_id = Customers.cust_id)\n                   AS orders_num\nFROM Customers\nORDER BY cust_name;\n<\/pre>\n<h1>\u5341\u4e94\u3001\u8fde\u63a5<\/h1>\n<p>\u8fde\u63a5\u7528\u4e8e\u8fde\u63a5\u591a\u4e2a\u8868\uff0c\u4f7f\u7528 JOIN \u5173\u952e\u5b57\uff0c\u5e76\u4e14\u6761\u4ef6\u8bed\u53e5\u4f7f\u7528 ON \u800c\u4e0d\u662f WHERE\u3002<\/p>\n<p>\u8fde\u63a5\u53ef\u4ee5\u66ff\u6362\u5b50\u67e5\u8be2\uff0c\u5e76\u4e14\u6bd4\u5b50\u67e5\u8be2\u7684\u6548\u7387\u4e00\u822c\u4f1a\u66f4\u5feb\u3002<\/p>\n<p>\u53ef\u4ee5\u7528 AS \u7ed9\u5217\u540d\u3001\u8ba1\u7b97\u5b57\u6bb5\u548c\u8868\u540d\u53d6\u522b\u540d\uff0c\u7ed9\u8868\u540d\u53d6\u522b\u540d\u662f\u4e3a\u4e86\u7b80\u5316 SQL \u8bed\u53e5\u4ee5\u53ca\u8fde\u63a5\u76f8\u540c\u8868\u3002<\/p>\n<h2>\u5185\u8fde\u63a5<\/h2>\n<p>\u5185\u8fde\u63a5\u53c8\u79f0\u7b49\u503c\u8fde\u63a5\uff0c\u4f7f\u7528 INNER JOIN \u5173\u952e\u5b57\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT A.value, B.value\nFROM tablea AS A INNER JOIN tableb AS B\nON A.key = B.key;\n<\/pre>\n<p>\u53ef\u4ee5\u4e0d\u660e\u786e\u4f7f\u7528 INNER JOIN\uff0c\u800c\u4f7f\u7528\u666e\u901a\u67e5\u8be2\u5e76\u5728 WHERE \u4e2d\u5c06\u4e24\u4e2a\u8868\u4e2d\u8981\u8fde\u63a5\u7684\u5217\u7528\u7b49\u503c\u65b9\u6cd5\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT A.value, B.value\nFROM tablea AS A, tableb AS B\nWHERE A.key = B.key;\n<\/pre>\n<p>\u5728\u6ca1\u6709\u6761\u4ef6\u8bed\u53e5\u7684\u60c5\u51b5\u4e0b\u8fd4\u56de\u7b1b\u5361\u5c14\u79ef\u3002<\/p>\n<h2>\u81ea\u8fde\u63a5<\/h2>\n<p>\u81ea\u8fde\u63a5\u53ef\u4ee5\u770b\u6210\u5185\u8fde\u63a5\u7684\u4e00\u79cd\uff0c\u53ea\u662f\u8fde\u63a5\u7684\u8868\u662f\u81ea\u8eab\u800c\u5df2\u3002<\/p>\n<p>\u4e00\u5f20\u5458\u5de5\u8868\uff0c\u5305\u542b\u5458\u5de5\u59d3\u540d\u548c\u5458\u5de5\u6240\u5c5e\u90e8\u95e8\uff0c\u8981\u627e\u51fa\u4e0e Jim \u5904\u5728\u540c\u4e00\u90e8\u95e8\u7684\u6240\u6709\u5458\u5de5\u59d3\u540d\u3002<\/p>\n<p>\u5b50\u67e5\u8be2\u7248\u672c<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT name\nFROM employee\nWHERE department = (\n      SELECT department\n      FROM employee\n      WHERE name = &amp;quot;Jim&amp;quot;);\n<\/pre>\n<p>\u81ea\u8fde\u63a5\u7248\u672c<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT e1.name\nFROM employee AS e1 INNER JOIN employee AS e2\nON e1.department = e2.department\n      AND e2.name = &amp;quot;Jim&amp;quot;;\n<\/pre>\n<h2>\u81ea\u7136\u8fde\u63a5<\/h2>\n<p>\u81ea\u7136\u8fde\u63a5\u662f\u628a\u540c\u540d\u5217\u901a\u8fc7\u7b49\u503c\u6d4b\u8bd5\u8fde\u63a5\u8d77\u6765\u7684\uff0c\u540c\u540d\u5217\u53ef\u4ee5\u6709\u591a\u4e2a\u3002<\/p>\n<p>\u5185\u8fde\u63a5\u548c\u81ea\u7136\u8fde\u63a5\u7684\u533a\u522b\uff1a\u5185\u8fde\u63a5\u63d0\u4f9b\u8fde\u63a5\u7684\u5217\uff0c\u800c\u81ea\u7136\u8fde\u63a5\u81ea\u52a8\u8fde\u63a5\u6240\u6709\u540c\u540d\u5217\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT A.value, B.value\nFROM tablea AS A NATURAL JOIN tableb AS B;\n<\/pre>\n<h2>\u5916\u8fde\u63a5<\/h2>\n<p>\u5916\u8fde\u63a5\u4fdd\u7559\u4e86\u6ca1\u6709\u5173\u8054\u7684\u90a3\u4e9b\u884c\u3002\u5206\u4e3a\u5de6\u5916\u8fde\u63a5\uff0c\u53f3\u5916\u8fde\u63a5\u4ee5\u53ca\u5168\u5916\u8fde\u63a5\uff0c\u5de6\u5916\u8fde\u63a5\u5c31\u662f\u4fdd\u7559\u5de6\u8868\u6ca1\u6709\u5173\u8054\u7684\u884c\u3002<\/p>\n<p>\u68c0\u7d22\u6240\u6709\u987e\u5ba2\u7684\u8ba2\u5355\u4fe1\u606f\uff0c\u5305\u62ec\u8fd8\u6ca1\u6709\u8ba2\u5355\u4fe1\u606f\u7684\u987e\u5ba2\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT Customers.cust_id, Orders.order_num\nFROM Customers LEFT OUTER JOIN Orders\nON Customers.cust_id = Orders.cust_id;\n<\/pre>\n<p>customers \u8868\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">cust_id<\/th>\n<th align=\"center\">cust_name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">a<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2<\/td>\n<td align=\"center\">b<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">3<\/td>\n<td align=\"center\">c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>orders \u8868\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">order_id<\/th>\n<th align=\"center\">cust_id<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">1<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2<\/td>\n<td align=\"center\">1<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">3<\/td>\n<td align=\"center\">3<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4<\/td>\n<td align=\"center\">3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ed3\u679c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">cust_id<\/th>\n<th align=\"center\">cust_name<\/th>\n<th align=\"center\">order_id<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">a<\/td>\n<td align=\"center\">1<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">1<\/td>\n<td align=\"center\">a<\/td>\n<td align=\"center\">2<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">3<\/td>\n<td align=\"center\">c<\/td>\n<td align=\"center\">3<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">3<\/td>\n<td align=\"center\">c<\/td>\n<td align=\"center\">4<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2<\/td>\n<td align=\"center\">b<\/td>\n<td align=\"center\">Null<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>\u5341\u516d\u3001\u7ec4\u5408\u67e5\u8be2<\/h1>\n<p>\u4f7f\u7528  <strong>UNION<\/strong>  \u6765\u7ec4\u5408\u4e24\u4e2a\u67e5\u8be2\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u67e5\u8be2\u8fd4\u56de M \u884c\uff0c\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u8fd4\u56de N \u884c\uff0c\u90a3\u4e48\u7ec4\u5408\u67e5\u8be2\u7684\u7ed3\u679c\u4e00\u822c\u4e3a M+N \u884c\u3002<\/p>\n<p>\u6bcf\u4e2a\u67e5\u8be2\u5fc5\u987b\u5305\u542b\u76f8\u540c\u7684\u5217\u3001\u8868\u8fbe\u5f0f\u548c\u805a\u96c6\u51fd\u6570\u3002<\/p>\n<p>\u9ed8\u8ba4\u4f1a\u53bb\u9664\u76f8\u540c\u884c\uff0c\u5982\u679c\u9700\u8981\u4fdd\u7559\u76f8\u540c\u884c\uff0c\u4f7f\u7528 UNION ALL\u3002<\/p>\n<p>\u53ea\u80fd\u5305\u542b\u4e00\u4e2a ORDER BY \u5b50\u53e5\uff0c\u5e76\u4e14\u5fc5\u987b\u4f4d\u4e8e\u8bed\u53e5\u7684\u6700\u540e\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT col\nFROM mytable\nWHERE col = 1\nUNION\nSELECT col\nFROM mytable\nWHERE col =2;\n<\/pre>\n<h1>\u5341\u4e03\u3001\u89c6\u56fe<\/h1>\n<p>\u89c6\u56fe\u662f\u865a\u62df\u7684\u8868\uff0c\u672c\u8eab\u4e0d\u5305\u542b\u6570\u636e\uff0c\u4e5f\u5c31\u4e0d\u80fd\u5bf9\u5176\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\u3002<\/p>\n<p>\u5bf9\u89c6\u56fe\u7684\u64cd\u4f5c\u548c\u5bf9\u666e\u901a\u8868\u7684\u64cd\u4f5c\u4e00\u6837\u3002<\/p>\n<p>\u89c6\u56fe\u5177\u6709\u5982\u4e0b\u597d\u5904\uff1a<\/p>\n<ul>\n<li>\u7b80\u5316\u590d\u6742\u7684 SQL \u64cd\u4f5c\uff0c\u6bd4\u5982\u590d\u6742\u7684\u8fde\u63a5\uff1b<\/li>\n<li>\u53ea\u4f7f\u7528\u5b9e\u9645\u8868\u7684\u4e00\u90e8\u5206\u6570\u636e\uff1b<\/li>\n<li>\u901a\u8fc7\u53ea\u7ed9\u7528\u6237\u8bbf\u95ee\u89c6\u56fe\u7684\u6743\u9650\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\uff1b<\/li>\n<li>\u66f4\u6539\u6570\u636e\u683c\u5f0f\u548c\u8868\u793a\u3002<\/li>\n<\/ul>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE VIEW myview AS\nSELECT Concat(col1, col2) AS concat_col, col3*col4 AS compute_col\nFROM mytable\nWHERE col5 = val;\n<\/pre>\n<h1>\u5341\u516b\u3001\u5b58\u50a8\u8fc7\u7a0b<\/h1>\n<p>\u5b58\u50a8\u8fc7\u7a0b\u53ef\u4ee5\u770b\u6210\u662f\u5bf9\u4e00\u7cfb\u5217 SQL \u64cd\u4f5c\u7684\u6279\u5904\u7406\u3002<\/p>\n<p>\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\u7684\u597d\u5904\uff1a<\/p>\n<ul>\n<li>\u4ee3\u7801\u5c01\u88c5\uff0c\u4fdd\u8bc1\u4e86\u4e00\u5b9a\u7684\u5b89\u5168\u6027\uff1b<\/li>\n<li>\u4ee3\u7801\u590d\u7528\uff1b<\/li>\n<li>\u7531\u4e8e\u662f\u9884\u5148\u7f16\u8bd1\uff0c\u56e0\u6b64\u5177\u6709\u5f88\u9ad8\u7684\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u547d\u4ee4\u884c\u4e2d\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u9700\u8981\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\uff0c\u56e0\u4e3a\u547d\u4ee4\u884c\u662f\u4ee5 ; \u4e3a\u7ed3\u675f\u7b26\uff0c\u800c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u4e5f\u5305\u542b\u4e86\u5206\u53f7\uff0c\u56e0\u6b64\u4f1a\u9519\u8bef\u628a\u8fd9\u90e8\u5206\u5206\u53f7\u5f53\u6210\u662f\u7ed3\u675f\u7b26\uff0c\u9020\u6210\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p>\u5305\u542b in\u3001out \u548c inout \u4e09\u79cd\u53c2\u6570\u3002<\/p>\n<p>\u7ed9\u53d8\u91cf\u8d4b\u503c\u90fd\u9700\u8981\u7528 select into \u8bed\u53e5\u3002<\/p>\n<p>\u6bcf\u6b21\u53ea\u80fd\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c\uff0c\u4e0d\u652f\u6301\u96c6\u5408\u7684\u64cd\u4f5c\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\ndelimiter \/\/\n\ncreate procedure myprocedure( out ret int )\n    begin\n        declare y int;\n        select sum(col1)\n        from mytable\n        into y;\n        select y*y into ret;\n    end \/\/\n\ndelimiter ;\n<\/pre>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\ncall myprocedure(@ret);\nselect @ret;\n<\/pre>\n<h1>\u5341\u4e5d\u3001\u6e38\u6807<\/h1>\n<p>\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u6e38\u6807\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u7ed3\u679c\u96c6\u8fdb\u884c\u79fb\u52a8\u904d\u5386\u3002<\/p>\n<p>\u6e38\u6807\u4e3b\u8981\u7528\u4e8e\u4ea4\u4e92\u5f0f\u5e94\u7528\uff0c\u5176\u4e2d\u7528\u6237\u9700\u8981\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u4efb\u610f\u884c\u8fdb\u884c\u6d4f\u89c8\u548c\u4fee\u6539\u3002<\/p>\n<p>\u4f7f\u7528\u6e38\u6807\u7684\u56db\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u58f0\u660e\u6e38\u6807\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u6ca1\u6709\u5b9e\u9645\u68c0\u7d22\u51fa\u6570\u636e\uff1b<\/li>\n<li>\u6253\u5f00\u6e38\u6807\uff1b<\/li>\n<li>\u53d6\u51fa\u6570\u636e\uff1b<\/li>\n<li>\u5173\u95ed\u6e38\u6807\uff1b<\/li>\n<\/ol>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\ndelimiter \/\/\ncreate procedure myprocedure(out ret int)\n    begin\n        declare done boolean default 0;\n\n        declare mycursor cursor for\n        select col1 from mytable;\n        # \u5b9a\u4e49\u4e86\u4e00\u4e2a continue handler\uff0c\u5f53 sqlstate &amp;#039;02000&amp;#039; \u8fd9\u4e2a\u6761\u4ef6\u51fa\u73b0\u65f6\uff0c\u4f1a\u6267\u884c set done = 1\n        declare continue handler for sqlstate &amp;#039;02000&amp;#039; set done = 1;\n\n        open mycursor;\n\n        repeat\n            fetch mycursor into ret;\n            select ret;\n        until done end repeat;\n\n        close mycursor;\n    end \/\/\n delimiter ;\n<\/pre>\n<h1>\u4e8c\u5341\u3001\u89e6\u53d1\u5668<\/h1>\n<p>\u89e6\u53d1\u5668\u4f1a\u5728\u67d0\u4e2a\u8868\u6267\u884c\u4ee5\u4e0b\u8bed\u53e5\u65f6\u800c\u81ea\u52a8\u6267\u884c\uff1aDELETE\u3001INSERT\u3001UPDATE\u3002<\/p>\n<p>\u89e6\u53d1\u5668\u5fc5\u987b\u6307\u5b9a\u5728\u8bed\u53e5\u6267\u884c\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\u81ea\u52a8\u6267\u884c\uff0c\u4e4b\u524d\u6267\u884c\u4f7f\u7528 BEFORE \u5173\u952e\u5b57\uff0c\u4e4b\u540e\u6267\u884c\u4f7f\u7528 AFTER \u5173\u952e\u5b57\u3002BEFORE \u7528\u4e8e\u6570\u636e\u9a8c\u8bc1\u548c\u51c0\u5316\uff0cAFTER \u7528\u4e8e\u5ba1\u8ba1\u8ddf\u8e2a\uff0c\u5c06\u4fee\u6539\u8bb0\u5f55\u5230\u53e6\u5916\u4e00\u5f20\u8868\u4e2d\u3002<\/p>\n<p>INSERT \u89e6\u53d1\u5668\u5305\u542b\u4e00\u4e2a\u540d\u4e3a NEW \u7684\u865a\u62df\u8868\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE TRIGGER mytrigger AFTER INSERT ON mytable\nFOR EACH ROW SELECT NEW.col into @result;\n\nSELECT @result; -- \u83b7\u53d6\u7ed3\u679c\n<\/pre>\n<p>DELETE \u89e6\u53d1\u5668\u5305\u542b\u4e00\u4e2a\u540d\u4e3a OLD \u7684\u865a\u62df\u8868\uff0c\u5e76\u4e14\u662f\u53ea\u8bfb\u7684\u3002<\/p>\n<p>UPDATE \u89e6\u53d1\u5668\u5305\u542b\u4e00\u4e2a\u540d\u4e3a NEW \u548c\u4e00\u4e2a\u540d\u4e3a OLD \u7684\u865a\u62df\u8868\uff0c\u5176\u4e2d NEW \u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\uff0c\u800c OLD \u662f\u53ea\u8bfb\u7684\u3002<\/p>\n<p>MySQL \u4e0d\u5141\u8bb8\u5728\u89e6\u53d1\u5668\u4e2d\u4f7f\u7528 CALL \u8bed\u53e5\uff0c\u4e5f\u5c31\u662f\u4e0d\u80fd\u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b\u3002<\/p>\n<h1>\u4e8c\u5341\u4e00\u3001\u4e8b\u52a1\u7ba1\u7406<\/h1>\n<p>\u57fa\u672c\u672f\u8bed\uff1a<\/p>\n<ul>\n<li>\u4e8b\u52a1\uff08transaction\uff09\u6307\u4e00\u7ec4 SQL \u8bed\u53e5\uff1b<\/li>\n<li>\u56de\u9000\uff08rollback\uff09\u6307\u64a4\u9500\u6307\u5b9a SQL \u8bed\u53e5\u7684\u8fc7\u7a0b\uff1b<\/li>\n<li>\u63d0\u4ea4\uff08commit\uff09\u6307\u5c06\u672a\u5b58\u50a8\u7684 SQL \u8bed\u53e5\u7ed3\u679c\u5199\u5165\u6570\u636e\u5e93\u8868\uff1b<\/li>\n<li>\u4fdd\u7559\u70b9\uff08savepoint\uff09\u6307\u4e8b\u52a1\u5904\u7406\u4e2d\u8bbe\u7f6e\u7684\u4e34\u65f6\u5360\u4f4d\u7b26\uff08placeholder\uff09\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b83\u53d1\u5e03\u56de\u9000\uff08\u4e0e\u56de\u9000\u6574\u4e2a\u4e8b\u52a1\u5904\u7406\u4e0d\u540c\uff09\u3002<\/li>\n<\/ul>\n<p>\u4e0d\u80fd\u56de\u9000 SELECT \u8bed\u53e5\uff0c\u56de\u9000 SELECT \u8bed\u53e5\u4e5f\u6ca1\u610f\u4e49\uff1b\u4e5f\u4e0d\u80fd\u56de\u9000 CREATE \u548c DROP \u8bed\u53e5\u3002<\/p>\n<p>MySQL \u7684\u4e8b\u52a1\u63d0\u4ea4\u9ed8\u8ba4\u662f\u9690\u5f0f\u63d0\u4ea4\uff0c\u6bcf\u6267\u884c\u4e00\u6761\u8bed\u53e5\u5c31\u628a\u8fd9\u6761\u8bed\u53e5\u5f53\u6210\u4e00\u4e2a\u4e8b\u52a1\u7136\u540e\u8fdb\u884c\u63d0\u4ea4\u3002\u5f53\u51fa\u73b0 START TRANSACTION \u8bed\u53e5\u65f6\uff0c\u4f1a\u5173\u95ed\u9690\u5f0f\u63d0\u4ea4\uff1b\u5f53 COMMIT \u6216 ROLLBACK \u8bed\u53e5\u6267\u884c\u540e\uff0c\u4e8b\u52a1\u4f1a\u81ea\u52a8\u5173\u95ed\uff0c\u91cd\u65b0\u6062\u590d\u9690\u5f0f\u63d0\u4ea4\u3002<\/p>\n<p>\u901a\u8fc7\u8bbe\u7f6e autocommit \u4e3a 0 \u53ef\u4ee5\u53d6\u6d88\u81ea\u52a8\u63d0\u4ea4\uff1bautocommit \u6807\u8bb0\u662f\u9488\u5bf9\u6bcf\u4e2a\u8fde\u63a5\u800c\u4e0d\u662f\u9488\u5bf9\u670d\u52a1\u5668\u7684\u3002<\/p>\n<p>\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u4fdd\u7559\u70b9\uff0cROLLBACK \u4f1a\u56de\u9000\u5230 START TRANSACTION \u8bed\u53e5\u5904\uff1b\u5982\u679c\u8bbe\u7f6e\u4e86\u4fdd\u7559\u70b9\uff0c\u5e76\u4e14\u5728 ROLLBACK \u4e2d\u6307\u5b9a\u8be5\u4fdd\u7559\u70b9\uff0c\u5219\u4f1a\u56de\u9000\u5230\u8be5\u4fdd\u7559\u70b9\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSTART TRANSACTION\n\/\/ ...\nSAVEPOINT delete1\n\/\/ ...\nROLLBACK TO delete1\n\/\/ ...\nCOMMIT\n<\/pre>\n<h1>\u4e8c\u5341\u4e8c\u3001\u5b57\u7b26\u96c6<\/h1>\n<p>\u57fa\u672c\u672f\u8bed\uff1a<\/p>\n<ul>\n<li>\u5b57\u7b26\u96c6\u4e3a\u5b57\u6bcd\u548c\u7b26\u53f7\u7684\u96c6\u5408\uff1b<\/li>\n<li>\u7f16\u7801\u4e3a\u67d0\u4e2a\u5b57\u7b26\u96c6\u6210\u5458\u7684\u5185\u90e8\u8868\u793a\uff1b<\/li>\n<li>\u6821\u5bf9\u5b57\u7b26\u6307\u5b9a\u5982\u4f55\u6bd4\u8f83\uff0c\u4e3b\u8981\u7528\u4e8e\u6392\u5e8f\u548c\u5206\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u9664\u4e86\u7ed9\u8868\u6307\u5b9a\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u5916\uff0c\u4e5f\u53ef\u4ee5\u7ed9\u5217\u6307\u5b9a\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE TABLE mytable\n(col VARCHAR(10) CHARACTER SET latin COLLATE latin1_general_ci )\nDEFAULT CHARACTER SET hebrew COLLATE hebrew_general_ci;\n<\/pre>\n<p>\u53ef\u4ee5\u5728\u6392\u5e8f\u3001\u5206\u7ec4\u65f6\u6307\u5b9a\u6821\u5bf9\uff1a<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM mytable\nORDER BY col COLLATE latin1_general_ci;\n<\/pre>\n<h1>\u4e8c\u5341\u4e09\u3001\u6743\u9650\u7ba1\u7406<\/h1>\n<p>MySQL \u7684\u8d26\u6237\u4fe1\u606f\u4fdd\u5b58\u5728 mysql \u8fd9\u4e2a\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nUSE mysql;\nSELECT user FROM user;\n<\/pre>\n<p><strong>\u521b\u5efa\u8d26\u6237<\/strong><\/p>\n<p>\u65b0\u521b\u5efa\u7684\u8d26\u6237\u6ca1\u6709\u4efb\u4f55\u6743\u9650\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE USER myuser IDENTIFIED BY &amp;#039;mypassword&amp;#039;;\n<\/pre>\n<p><strong>\u4fee\u6539\u8d26\u6237\u540d<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nRENAME myuser TO newuser;\n<\/pre>\n<p><strong>\u5220\u9664\u8d26\u6237<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nDROP USER myuser;\n<\/pre>\n<p><strong>\u67e5\u770b\u6743\u9650<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSHOW GRANTS FOR myuser;\n<\/pre>\n<p><strong>\u6388\u4e88\u6743\u9650<\/strong><\/p>\n<p>\u8d26\u6237\u7528 username@host \u7684\u5f62\u5f0f\u5b9a\u4e49\uff0cusername@% \u4f7f\u7528\u7684\u662f\u9ed8\u8ba4\u4e3b\u673a\u540d\u3002<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nGRANT SELECT, INSERT ON mydatabase.* TO myuser;\n<\/pre>\n<p><strong>\u5220\u9664\u6743\u9650<\/strong><\/p>\n<p>GRANT \u548c REVOKE \u53ef\u5728\u51e0\u4e2a\u5c42\u6b21\u4e0a\u63a7\u5236\u8bbf\u95ee\u6743\u9650\uff1a<\/p>\n<ul>\n<li>\u6574\u4e2a\u670d\u52a1\u5668\uff0c\u4f7f\u7528 GRANT ALL \u548c REVOKE ALL\uff1b<\/li>\n<li>\u6574\u4e2a\u6570\u636e\u5e93\uff0c\u4f7f\u7528 ON database.&#42;\uff1b<\/li>\n<li>\u7279\u5b9a\u7684\u8868\uff0c\u4f7f\u7528 ON database.table\uff1b<\/li>\n<li>\u7279\u5b9a\u7684\u5217\uff1b<\/li>\n<li>\u7279\u5b9a\u7684\u5b58\u50a8\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nREVOKE SELECT, INSERT ON mydatabase.* FROM myuser;\n<\/pre>\n<p><strong>\u66f4\u6539\u5bc6\u7801<\/strong><\/p>\n<p>\u5fc5\u987b\u4f7f\u7528 Password() \u51fd\u6570<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSET PASSWROD FOR myuser = Password(&amp;#039;new_password&amp;#039;);\n<\/pre>\n<h1>\u53c2\u8003\u8d44\u6599<\/h1>\n<ul>\n<li>BenForta. SQL \u5fc5\u77e5\u5fc5\u4f1a [M]. \u4eba\u6c11\u90ae\u7535\u51fa\u7248\u793e, 2013.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003 \u4e00\u3001\u57fa\u7840 \u6a21\u5f0f\u5b9a\u4e49\u4e86\u6570\u636e\u5982\u4f55\u5b58\u50a8\u3001\u5b58\u50a8\u4ec0\u4e48\u6837\u7684\u6570\u636e\u4ee5\u53ca\u6570\u636e\u5982\u4f55\u5206\u89e3\u7b49\u4fe1\u606f\uff0c\u6570\u636e\u5e93\u548c\u8868\u90fd\u6709\u6a21\u5f0f\u3002 \u4e3b\u952e\u7684\u503c<a href=\"https:\/\/www.ccagml.com\/?p=275\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SQL\u57fa\u7840\u77e5\u8bc6<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,22],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/275"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=275"}],"version-history":[{"count":3,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}