{"id":160749,"date":"2024-11-04T00:41:22","date_gmt":"2024-11-03T16:41:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160749\/"},"modified":"2024-11-04T00:41:23","modified_gmt":"2024-11-03T16:41:23","slug":"%e8%b3%87%e6%96%99%e5%ba%ab%e6%95%99%e7%a8%8b%ef%bc%9amysql%e8%ae%8a%e9%87%8f%e7%9a%84%e5%ae%9a%e7%be%a9%e5%92%8c%e8%b3%a6%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160749\/","title":{"rendered":"\u8cc7\u6599\u5eab\u6559\u7a0b\uff1aMySQL\u8b8a\u91cf\u7684\u5b9a\u7fa9\u548c\u8ce6\u503c"},"content":{"rendered":"<h1 id=\"%e8%b3%87%e6%96%99%e5%ba%ab%e6%95%99%e7%a8%8b%ef%bc%9amysql%e8%ae%8a%e9%87%8f%e7%9a%84%e5%ae%9a%e7%be%a9%e5%92%8c%e8%b3%a6%e5%80%bc-dnlJzXmtQi\">\u8cc7\u6599\u5eab\u6559\u7a0b\uff1aMySQL\u8b8a\u91cf\u7684\u5b9a\u7fa9\u548c\u8ce6\u503c<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u8b8a\u91cf\u7684\u4f7f\u7528\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u7279\u5225\u662f\u5728MySQL\u4e2d\u3002\u8b8a\u91cf\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u5728\u67e5\u8a62\u4e2d\u5b58\u5132\u548c\u64cd\u4f5c\u6578\u64da\uff0c\u5f9e\u800c\u63d0\u9ad8\u67e5\u8a62\u7684\u9748\u6d3b\u6027\u548c\u6548\u7387\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u8b8a\u91cf\u7684\u5b9a\u7fa9\u548c\u8ce6\u503c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u4f86\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u4e3b\u984c\u3002<\/p>\n<h2 id=\"mysql%e8%ae%8a%e9%87%8f%e7%9a%84%e9%a1%9e%e5%9e%8b-dnlJzXmtQi\">MySQL\u8b8a\u91cf\u7684\u985e\u578b<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u8b8a\u91cf\u4e3b\u8981\u5206\u70ba\u5169\u7a2e\u985e\u578b\uff1a\u7528\u6236\u5b9a\u7fa9\u8b8a\u91cf\u548c\u7cfb\u7d71\u8b8a\u91cf\u3002<\/p>\n<h3 id=\"1-%e7%94%a8%e6%88%b6%e5%ae%9a%e7%be%a9%e8%ae%8a%e9%87%8f-dnlJzXmtQi\">1. \u7528\u6236\u5b9a\u7fa9\u8b8a\u91cf<\/h3>\n<p>\u7528\u6236\u5b9a\u7fa9\u8b8a\u91cf\u662f\u7531\u7528\u6236\u5275\u5efa\u7684\u8b8a\u91cf\uff0c\u901a\u5e38\u4ee5\u300c@\u300d\u7b26\u865f\u958b\u982d\u3002\u9019\u4e9b\u8b8a\u91cf\u7684\u4f5c\u7528\u7bc4\u570d\u662f\u6703\u8a71\u7d1a\u5225\u7684\uff0c\u610f\u5473\u8457\u5b83\u5011\u5728\u7576\u524d\u6703\u8a71\u4e2d\u6709\u6548\uff0c\u4e26\u4e14\u5728\u6703\u8a71\u7d50\u675f\u5f8c\u6703\u88ab\u81ea\u52d5\u522a\u9664\u3002<\/p>\n<pre><code>SET @my_variable = 10;\nSELECT @my_variable;\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u4f7f\u7528SET\u8a9e\u53e5\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba<code>@my_variable<\/code>\u7684\u8b8a\u91cf\uff0c\u4e26\u5c07\u5176\u8ce6\u503c\u70ba10\u3002\u63a5\u8457\uff0c\u6211\u5011\u4f7f\u7528SELECT\u8a9e\u53e5\u4f86\u67e5\u8a62\u9019\u500b\u8b8a\u91cf\u7684\u503c\u3002<\/p>\n<h3 id=\"2-%e7%b3%bb%e7%b5%b1%e8%ae%8a%e9%87%8f-dnlJzXmtQi\">2. \u7cfb\u7d71\u8b8a\u91cf<\/h3>\n<p>\u7cfb\u7d71\u8b8a\u91cf\u662fMySQL\u5167\u90e8\u4f7f\u7528\u7684\u8b8a\u91cf\uff0c\u9019\u4e9b\u8b8a\u91cf\u7684\u503c\u53ef\u4ee5\u5f71\u97ffMySQL\u7684\u884c\u70ba\u3002\u7cfb\u7d71\u8b8a\u91cf\u53ef\u4ee5\u662f\u5168\u5c40\u7684\u6216\u6703\u8a71\u7684\uff0c\u4e26\u4e14\u53ef\u4ee5\u901a\u904e<code>SHOW VARIABLES<\/code>\u547d\u4ee4\u4f86\u67e5\u770b\u3002<\/p>\n<pre><code>SHOW VARIABLES LIKE 'max_connections';\n<\/code><\/pre>\n<p>\u9019\u689d\u547d\u4ee4\u5c07\u986f\u793a\u7576\u524dMySQL\u4f3a\u670d\u5668\u7684\u6700\u5927\u9023\u63a5\u6578\u8a2d\u7f6e\u3002<\/p>\n<h2 id=\"%e8%ae%8a%e9%87%8f%e7%9a%84%e8%b3%a6%e5%80%bc-dnlJzXmtQi\">\u8b8a\u91cf\u7684\u8ce6\u503c<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u8b8a\u91cf\u7684\u8ce6\u503c\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u65b9\u5f0f\u9032\u884c\u3002\u9664\u4e86\u4f7f\u7528<code>SET<\/code>\u8a9e\u53e5\u5916\uff0c\u9084\u53ef\u4ee5\u5728\u67e5\u8a62\u4e2d\u76f4\u63a5\u8ce6\u503c\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8select%e8%aa%9e%e5%8f%a5%e8%b3%a6%e5%80%bc-dnlJzXmtQi\">\u4f7f\u7528SELECT\u8a9e\u53e5\u8ce6\u503c<\/h3>\n<p>\u7528\u6236\u5b9a\u7fa9\u8b8a\u91cf\u4e5f\u53ef\u4ee5\u901a\u904e<code>SELECT<\/code>\u8a9e\u53e5\u9032\u884c\u8ce6\u503c\uff0c\u9019\u5728\u9700\u8981\u5f9e\u67e5\u8a62\u7d50\u679c\u4e2d\u7372\u53d6\u503c\u6642\u7279\u5225\u6709\u7528\u3002<\/p>\n<pre><code>SELECT @my_variable := COUNT(*) FROM my_table;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528<code>SELECT<\/code>\u8a9e\u53e5\u4f86\u8a08\u7b97<code>my_table<\/code>\u8868\u4e2d\u7684\u884c\u6578\uff0c\u4e26\u5c07\u7d50\u679c\u8ce6\u503c\u7d66<code>@my_variable<\/code>\u3002<\/p>\n<h3 id=\"%e8%ae%8a%e9%87%8f%e7%9a%84%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-dnlJzXmtQi\">\u8b8a\u91cf\u7684\u4f7f\u7528\u7bc4\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u7528\u6236\u5b9a\u7fa9\u8b8a\u91cf\u7684\u5be6\u969b\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528\u8b8a\u91cf\u4f86\u8a08\u7b97\u7e3d\u548c\u3002<\/p>\n<pre><code>SET @total := 0;\nSELECT @total := SUM(price) FROM orders WHERE status = 'completed';\nSELECT @total;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u9996\u5148\u521d\u59cb\u5316<code>@total<\/code>\u8b8a\u91cf\u70ba0\uff0c\u7136\u5f8c\u8a08\u7b97<code>orders<\/code>\u8868\u4e2d\u6240\u6709\u5df2\u5b8c\u6210\u8a02\u55ae\u7684\u50f9\u683c\u7e3d\u548c\uff0c\u4e26\u5c07\u7d50\u679c\u8ce6\u503c\u7d66<code>@total<\/code>\u3002\u6700\u5f8c\uff0c\u6211\u5011\u4f7f\u7528<code>SELECT<\/code>\u8a9e\u53e5\u4f86\u67e5\u8a62<code>@total<\/code>\u7684\u503c\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-dnlJzXmtQi\">\u7d50\u8ad6<\/h2>\n<p>MySQL\u8b8a\u91cf\u7684\u5b9a\u7fa9\u548c\u8ce6\u503c\u662f\u8cc7\u6599\u5eab\u64cd\u4f5c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u901a\u904e\u9748\u6d3b\u5730\u4f7f\u7528\u7528\u6236\u5b9a\u7fa9\u8b8a\u91cf\u548c\u7cfb\u7d71\u8b8a\u91cf\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8655\u7406\u6578\u64da\u548c\u57f7\u884c\u67e5\u8a62\u3002\u7121\u8ad6\u662f\u5728\u7c21\u55ae\u7684\u67e5\u8a62\u9084\u662f\u8907\u96dc\u7684\u6578\u64da\u8655\u7406\u904e\u7a0b\u4e2d\uff0c\u8b8a\u91cf\u90fd\u80fd\u63d0\u4f9b\u6975\u5927\u7684\u4fbf\u5229\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u8cc7\u6599\u5eab\u7ba1\u7406\u6709\u9032\u4e00\u6b65\u7684\u9700\u6c42\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684\u8cc7\u6599\u5eab\u74b0\u5883\uff0c\u4eab\u53d7\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u8b8a\u91cf\u7684\u5b9a\u7fa9\u548c\u8ce6\u503c\uff0c\u638c\u63e1\u8cc7\u6599\u5eab\u64cd\u4f5c\u7684\u57fa\u790e\u77e5\u8b58\uff0c\u63d0\u5347\u60a8\u7684\u6578\u64da\u7ba1\u7406\u6280\u80fd\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-160749","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160749","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=160749"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160749\/revisions"}],"predecessor-version":[{"id":160750,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160749\/revisions\/160750"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}