{"id":168453,"date":"2024-11-06T12:30:25","date_gmt":"2024-11-06T04:30:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168453\/"},"modified":"2024-11-06T12:30:25","modified_gmt":"2024-11-06T04:30:25","slug":"sql-%e8%aa%9e%e5%8f%a5%e7%8d%b2%e5%8f%96%e5%b9%b4%e6%9c%88%e6%97%a5%e6%9c%9f%e5%87%bd%e6%95%b8%ef%bc%8c%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168453\/","title":{"rendered":"SQL \u8a9e\u53e5\u7372\u53d6\u5e74\u6708\u65e5\u671f\u51fd\u6578\uff0c\u5be6\u73fe\u6578\u64da\u5eab\u6578\u64da\u8655\u7406 (\u6578\u64da\u5eab\u65e5\u671f\u53d6\u5e74\u6708\u51fd\u6578)"},"content":{"rendered":"<h1 id=\"sql-%e8%aa%9e%e5%8f%a5%e7%8d%b2%e5%8f%96%e5%b9%b4%e6%9c%88%e6%97%a5%e6%9c%9f%e5%87%bd%e6%95%b8%ef%bc%8c%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-nBiXbgJdHD\">SQL \u8a9e\u53e5\u7372\u53d6\u5e74\u6708\u65e5\u671f\u51fd\u6578\uff0c\u5be6\u73fe\u6578\u64da\u5eab\u6578\u64da\u8655\u7406<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0c\u65e5\u671f\u548c\u6642\u9593\u7684\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u74b0\u3002\u7121\u8ad6\u662f\u9032\u884c\u6578\u64da\u5206\u6790\u3001\u5831\u8868\u751f\u6210\uff0c\u9084\u662f\u6578\u64da\u6e05\u7406\uff0c\u5c0d\u65bc\u65e5\u671f\u7684\u64cd\u4f5c\u90fd\u4e0d\u53ef\u6216\u7f3a\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 SQL \u8a9e\u53e5\u7372\u53d6\u5e74\u6708\u65e5\u671f\u51fd\u6578\uff0c\u4ee5\u5be6\u73fe\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u8655\u7406\u3002<\/p>\n<h2 id=\"%e6%97%a5%e6%9c%9f%e5%87%bd%e6%95%b8%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-nBiXbgJdHD\">\u65e5\u671f\u51fd\u6578\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c\u65e5\u671f\u51fd\u6578\u662f\u7528\u4f86\u8655\u7406\u65e5\u671f\u548c\u6642\u9593\u6578\u64da\u7684\u5de5\u5177\u3002\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u7528\u6236\u5f9e\u65e5\u671f\u4e2d\u63d0\u53d6\u51fa\u7279\u5b9a\u7684\u90e8\u5206\uff0c\u4f8b\u5982\u5e74\u3001\u6708\u3001\u65e5\u7b49\u3002\u4e0d\u540c\u7684\u6578\u64da\u5eab\u7cfb\u7d71\uff08\u5982 MySQL\u3001SQL Server\u3001Oracle \u7b49\uff09\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u65e5\u671f\u51fd\u6578\uff0c\u4f46\u5b83\u5011\u7684\u57fa\u672c\u529f\u80fd\u5927\u81f4\u76f8\u540c\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e7%94%a8%e7%9a%84%e6%97%a5%e6%9c%9f%e5%87%bd%e6%95%b8-nBiXbgJdHD\">\u5e38\u7528\u7684\u65e5\u671f\u51fd\u6578<\/h2>\n<ul>\n<li><strong>YEAR()<\/strong>\uff1a\u63d0\u53d6\u5e74\u4efd<\/li>\n<li><strong>MONTH()<\/strong>\uff1a\u63d0\u53d6\u6708\u4efd<\/li>\n<li><strong>DAY()<\/strong>\uff1a\u63d0\u53d6\u65e5\u671f<\/li>\n<li><strong>DATE_FORMAT()<\/strong>\uff1a\u683c\u5f0f\u5316\u65e5\u671f<\/li>\n<\/ul>\n<h2 id=\"mysql-%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e8%99%95%e7%90%86-nBiXbgJdHD\">MySQL \u4e2d\u7684\u65e5\u671f\u8655\u7406<\/h2>\n<p>\u5728 MySQL \u4e2d\uff0c\u7372\u53d6\u5e74\u6708\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528 <code>YEAR()<\/code> \u548c <code>MONTH()<\/code> \u51fd\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre><code>SELECT YEAR(NOW()) AS current_year, MONTH(NOW()) AS current_month;<\/code><\/pre>\n<p>\u9019\u689d SQL \u8a9e\u53e5\u5c07\u8fd4\u56de\u7576\u524d\u7684\u5e74\u4efd\u548c\u6708\u4efd\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e5%be%9e%e6%95%b8%e6%93%9a%e8%a1%a8%e4%b8%ad%e6%8f%90%e5%8f%96%e5%b9%b4%e6%9c%88-nBiXbgJdHD\">\u793a\u4f8b\uff1a\u5f9e\u6578\u64da\u8868\u4e2d\u63d0\u53d6\u5e74\u6708<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>orders<\/code> \u7684\u6578\u64da\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u500b <code>order_date<\/code> \u5217\uff0c\u8a18\u9304\u4e86\u6bcf\u500b\u8a02\u55ae\u7684\u65e5\u671f\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u8a9e\u53e5\u4f86\u7372\u53d6\u6bcf\u500b\u8a02\u55ae\u7684\u5e74\u4efd\u548c\u6708\u4efd\uff1a<\/p>\n<pre><code>SELECT \n    YEAR(order_date) AS order_year, \n    MONTH(order_date) AS order_month, \n    COUNT(*) AS total_orders \nFROM \n    orders \nGROUP BY \n    order_year, order_month \nORDER BY \n    order_year, order_month;<\/code><\/pre>\n<p>\u9019\u689d\u8a9e\u53e5\u5c07\u8fd4\u56de\u6bcf\u500b\u5e74\u4efd\u548c\u6708\u4efd\u7684\u8a02\u55ae\u7e3d\u6578\uff0c\u4e26\u6309\u5e74\u4efd\u548c\u6708\u4efd\u6392\u5e8f\u3002<\/p>\n<h2 id=\"sql-server-%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e8%99%95%e7%90%86-nBiXbgJdHD\">SQL Server \u4e2d\u7684\u65e5\u671f\u8655\u7406<\/h2>\n<p>\u5728 SQL Server \u4e2d\uff0c\u7372\u53d6\u5e74\u6708\u7684\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\u3002\u53ef\u4ee5\u4f7f\u7528 <code>YEAR()<\/code> \u548c <code>MONTH()<\/code> \u51fd\u6578\uff0c\u4ee5\u4e0b\u662f\u793a\u4f8b\uff1a<\/p>\n<pre><code>SELECT \n    YEAR(GETDATE()) AS current_year, \n    MONTH(GETDATE()) AS current_month;<\/code><\/pre>\n<p>\u540c\u6a23\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u5f9e\u6578\u64da\u8868\u4e2d\u63d0\u53d6\u5e74\u6708\uff1a<\/p>\n<pre><code>SELECT \n    YEAR(order_date) AS order_year, \n    MONTH(order_date) AS order_month, \n    COUNT(*) AS total_orders \nFROM \n    orders \nGROUP BY \n    YEAR(order_date), MONTH(order_date) \nORDER BY \n    order_year, order_month;<\/code><\/pre>\n<h2 id=\"oracle-%e4%b8%ad%e7%9a%84%e6%97%a5%e6%9c%9f%e8%99%95%e7%90%86-nBiXbgJdHD\">Oracle \u4e2d\u7684\u65e5\u671f\u8655\u7406<\/h2>\n<p>\u5728 Oracle \u6578\u64da\u5eab\u4e2d\uff0c\u65e5\u671f\u8655\u7406\u4f7f\u7528 <code>EXTRACT()<\/code> \u51fd\u6578\u4f86\u7372\u53d6\u5e74\u4efd\u548c\u6708\u4efd\uff1a<\/p>\n<pre><code>SELECT \n    EXTRACT(YEAR FROM SYSDATE) AS current_year, \n    EXTRACT(MONTH FROM SYSDATE) AS current_month \nFROM \n    dual;<\/code><\/pre>\n<p>\u540c\u6a23\u5730\uff0c\u5f9e\u6578\u64da\u8868\u4e2d\u63d0\u53d6\u5e74\u6708\u7684 SQL \u8a9e\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT \n    EXTRACT(YEAR FROM order_date) AS order_year, \n    EXTRACT(MONTH FROM order_date) AS order_month, \n    COUNT(*) AS total_orders \nFROM \n    orders \nGROUP BY \n    EXTRACT(YEAR FROM order_date), EXTRACT(MONTH FROM order_date) \nORDER BY \n    order_year, order_month;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-nBiXbgJdHD\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u65e5\u671f\u7684\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u7121\u8ad6\u662f\u9032\u884c\u6578\u64da\u5206\u6790\u9084\u662f\u751f\u6210\u5831\u8868\uff0c\u5c0d\u65bc\u65e5\u671f\u7684\u64cd\u4f5c\u90fd\u4e0d\u53ef\u6216\u7f3a\u3002\u901a\u904e\u4f7f\u7528 SQL \u8a9e\u53e5\u4e2d\u7684\u65e5\u671f\u51fd\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u7372\u53d6\u5e74\u6708\u4fe1\u606f\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u6578\u64da\u8655\u7406\u3002\u7121\u8ad6\u60a8\u4f7f\u7528\u7684\u662f MySQL\u3001SQL Server \u9084\u662f Oracle\uff0c\u9019\u4e9b\u57fa\u672c\u7684\u65e5\u671f\u51fd\u6578\u90fd\u80fd\u5e6b\u52a9\u60a8\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u5206\u6790\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u9700\u6c42\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7528\u6236\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 SQL \u8a9e\u53e5\u4e2d\u7684\u5e74\u6708\u65e5\u671f\u51fd\u6578\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u6578\u64da\u8655\u7406\uff0c\u63d0\u5347\u6578\u64da\u5206\u6790\u6548\u7387\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-168453","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168453","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=168453"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168453\/revisions"}],"predecessor-version":[{"id":168454,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168453\/revisions\/168454"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}