{"id":193455,"date":"2024-11-12T22:19:26","date_gmt":"2024-11-12T14:19:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/193455\/"},"modified":"2024-11-12T22:19:28","modified_gmt":"2024-11-12T14:19:28","slug":"%e5%ad%b8%e7%bf%92mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e5%87%bd%e6%95%b8%e6%a0%bc%e5%bc%8f%ef%bc%8c%e8%bc%95%e9%ac%86%e6%93%8d%e6%8e%a7%e5%a4%a7%e6%95%b8%e6%93%9a-mysql%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/193455\/","title":{"rendered":"\u5b78\u7fd2MySQL\u6578\u64da\u5eab\u51fd\u6578\u683c\u5f0f\uff0c\u8f15\u9b06\u64cd\u63a7\u5927\u6578\u64da (mysql\u6578\u64da\u5eab\u51fd\u6578\u683c\u5f0f)"},"content":{"rendered":"<h1 id=\"%e5%ad%b8%e7%bf%92mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e5%87%bd%e6%95%b8%e6%a0%bc%e5%bc%8f%ef%bc%8c%e8%bc%95%e9%ac%86%e6%93%8d%e6%8e%a7%e5%a4%a7%e6%95%b8%e6%93%9a-iLDAsResCI\">\u5b78\u7fd2MySQL\u6578\u64da\u5eab\u51fd\u6578\u683c\u5f0f\uff0c\u8f15\u9b06\u64cd\u63a7\u5927\u6578\u64da<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u6642\u4ee3\uff0cMySQL\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u7db2\u7ad9\u9084\u662f\u5927\u578b\u4f01\u696d\u7cfb\u7d71\uff0cMySQL\u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u548c\u6aa2\u7d22\u529f\u80fd\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u6578\u64da\u5eab\u51fd\u6578\u7684\u683c\u5f0f\uff0c\u5e6b\u52a9\u8b80\u8005\u8f15\u9b06\u638c\u63e1\u5982\u4f55\u64cd\u63a7\u5927\u6578\u64da\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afmysql%e5%87%bd%e6%95%b8%ef%bc%9f-iLDAsResCI\">\u4ec0\u9ebc\u662fMySQL\u51fd\u6578\uff1f<\/h2>\n<p>MySQL\u51fd\u6578\u662f\u7528\u65bc\u57f7\u884c\u7279\u5b9a\u64cd\u4f5c\u7684\u9810\u5b9a\u7fa9\u4ee3\u78bc\u584a\u3002\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u6578\u64da\u8a08\u7b97\u3001\u5b57\u7b26\u4e32\u8655\u7406\u3001\u65e5\u671f\u548c\u6642\u9593\u64cd\u4f5c\u7b49\u3002MySQL\u63d0\u4f9b\u4e86\u591a\u7a2e\u5167\u5efa\u51fd\u6578\uff0c\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u7c21\u5316\u6578\u64da\u64cd\u4f5c\uff0c\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n<h2 id=\"mysql%e5%87%bd%e6%95%b8%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a0%bc%e5%bc%8f-iLDAsResCI\">MySQL\u51fd\u6578\u7684\u57fa\u672c\u683c\u5f0f<\/h2>\n<p>MySQL\u51fd\u6578\u7684\u57fa\u672c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>\u51fd\u6578\u540d\u7a31(\u53c3\u65781, \u53c3\u65782, ...)<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u51fd\u6578\u540d\u7a31\u662f\u4f60\u8981\u8abf\u7528\u7684\u51fd\u6578\u7684\u540d\u7a31\uff0c\u800c\u53c3\u6578\u5247\u662f\u50b3\u905e\u7d66\u51fd\u6578\u7684\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684MySQL\u51fd\u6578\u53ca\u5176\u7528\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e6%95%b8%e5%ad%b8%e5%87%bd%e6%95%b8-iLDAsResCI\">1. \u6578\u5b78\u51fd\u6578<\/h3>\n<p>MySQL\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u5b78\u51fd\u6578\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>ABS(x)<\/code>\uff1a\u8fd4\u56dex\u7684\u7d55\u5c0d\u503c\u3002<\/li>\n<li><code>ROUND(x, d)<\/code>\uff1a\u5c07x\u56db\u6368\u4e94\u5165\u5230d\u4f4d\u5c0f\u6578\u3002<\/li>\n<li><code>FLOOR(x)<\/code>\uff1a\u8fd4\u56de\u5c0f\u65bc\u6216\u7b49\u65bcx\u7684\u6700\u5927\u6574\u6578\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>ROUND<\/code>\u51fd\u6578\u4f86\u56db\u6368\u4e94\u5165\u4e00\u500b\u6578\u5b57\uff1a<\/p>\n<pre><code>SELECT ROUND(123.4567, 2); -- \u8fd4\u56de 123.46<\/code><\/pre>\n<h3 id=\"2-%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%87%bd%e6%95%b8-iLDAsResCI\">2. \u5b57\u7b26\u4e32\u51fd\u6578<\/h3>\n<p>\u5b57\u7b26\u4e32\u51fd\u6578\u7528\u65bc\u8655\u7406\u6587\u672c\u6578\u64da\uff0c\u5e38\u898b\u7684\u6709\uff1a<\/p>\n<ul>\n<li><code>CONCAT(str1, str2, ...)<\/code>\uff1a\u5c07\u591a\u500b\u5b57\u7b26\u4e32\u9023\u63a5\u6210\u4e00\u500b\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>LENGTH(str)<\/code>\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u9577\u5ea6\u3002<\/li>\n<li><code>UPPER(str)<\/code>\uff1a\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5927\u5beb\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>CONCAT<\/code>\u51fd\u6578\u4f86\u9023\u63a5\u5169\u500b\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>SELECT CONCAT('Hello', ' ', 'World'); -- \u8fd4\u56de 'Hello World'<\/code><\/pre>\n<h3 id=\"3-%e6%97%a5%e6%9c%9f%e5%92%8c%e6%99%82%e9%96%93%e5%87%bd%e6%95%b8-iLDAsResCI\">3. \u65e5\u671f\u548c\u6642\u9593\u51fd\u6578<\/h3>\n<p>MySQL\u9084\u63d0\u4f9b\u4e86\u591a\u7a2e\u65e5\u671f\u548c\u6642\u9593\u51fd\u6578\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>NOW()<\/code>\uff1a\u8fd4\u56de\u7576\u524d\u7684\u65e5\u671f\u548c\u6642\u9593\u3002<\/li>\n<li><code>CURDATE()<\/code>\uff1a\u8fd4\u56de\u7576\u524d\u7684\u65e5\u671f\u3002<\/li>\n<li><code>DATEDIFF(date1, date2)<\/code>\uff1a\u8fd4\u56de\u5169\u500b\u65e5\u671f\u4e4b\u9593\u7684\u5929\u6578\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>DATEDIFF<\/code>\u51fd\u6578\u4f86\u8a08\u7b97\u5169\u500b\u65e5\u671f\u4e4b\u9593\u7684\u5dee\u7570\uff1a<\/p>\n<pre><code>SELECT DATEDIFF('2023-10-01', '2023-09-01'); -- \u8fd4\u56de 30<\/code><\/pre>\n<h2 id=\"%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8-iLDAsResCI\">\u81ea\u5b9a\u7fa9\u51fd\u6578<\/h2>\n<p>\u9664\u4e86\u5167\u5efa\u51fd\u6578\uff0cMySQL\u9084\u5141\u8a31\u7528\u6236\u5275\u5efa\u81ea\u5b9a\u7fa9\u51fd\u6578\u3002\u9019\u5c0d\u65bc\u9700\u8981\u91cd\u8907\u4f7f\u7528\u7684\u7279\u5b9a\u908f\u8f2f\u975e\u5e38\u6709\u7528\u3002\u5275\u5efa\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE FUNCTION \u51fd\u6578\u540d\u7a31(\u53c3\u65781 \u985e\u578b, \u53c3\u65782 \u985e\u578b, ...)\nRETURNS \u8fd4\u56de\u985e\u578b\nBEGIN\n    -- \u51fd\u6578\u908f\u8f2f\n    RETURN \u8fd4\u56de\u503c;\nEND;<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5275\u5efa\u4e00\u500b\u8a08\u7b97\u5e73\u65b9\u7684\u81ea\u5b9a\u7fa9\u51fd\u6578\uff1a<\/p>\n<pre><code>CREATE FUNCTION SQUARE(x INT)\nRETURNS INT\nBEGIN\n    RETURN x * x;\nEND;<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-iLDAsResCI\">\u7d50\u8ad6<\/h2>\n<p>\u638c\u63e1MySQL\u6578\u64da\u5eab\u51fd\u6578\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u7121\u8ad6\u662f\u9032\u884c\u6578\u5b78\u8a08\u7b97\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u9084\u662f\u65e5\u671f\u8655\u7406\uff0cMySQL\u90fd\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u51fd\u6578\u5eab\u4f86\u6eff\u8db3\u9700\u6c42\u3002\u901a\u904e\u5b78\u7fd2\u548c\u5be6\u8e10\u9019\u4e9b\u51fd\u6578\uff0c\u958b\u767c\u8005\u80fd\u5920\u66f4\u8f15\u9b06\u5730\u64cd\u63a7\u5927\u6578\u64da\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684MySQL\u6578\u64da\u5eab\uff0c\u4eab\u53d7\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u548c\u5b58\u5132\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1MySQL\u6578\u64da\u5eab\u51fd\u6578\u683c\u5f0f\uff0c\u8f15\u9b06\u7ba1\u7406\u548c\u64cd\u63a7\u5927\u6578\u64da\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u52a9\u4f60\u6210\u70ba\u6578\u64da\u5c08\u5bb6\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-193455","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193455","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=193455"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193455\/revisions"}],"predecessor-version":[{"id":193456,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193455\/revisions\/193456"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=193455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=193455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=193455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}