{"id":643,"date":"2025-01-01T11:11:27","date_gmt":"2025-01-01T03:11:27","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=643"},"modified":"2025-01-01T11:11:27","modified_gmt":"2025-01-01T03:11:27","slug":"%e5%9c%a8-sql-%e4%b8%ad%e6%8f%90%e5%8f%96%e6%97%a5%e6%9c%9f%e7%9a%84%e5%91%a8%e6%95%b0%ef%bc%8c%e6%9c%89%e5%93%aa%e4%ba%9b%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/01\/%e5%9c%a8-sql-%e4%b8%ad%e6%8f%90%e5%8f%96%e6%97%a5%e6%9c%9f%e7%9a%84%e5%91%a8%e6%95%b0%ef%bc%8c%e6%9c%89%e5%93%aa%e4%ba%9b%e6%96%b9%e6%b3%95%ef%bc%9f\/","title":{"rendered":"\u5728 SQL \u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 SQL \u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u65f6\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u4f1a\u6709\u4e0d\u540c\u7684\u65b9\u6cd5\u3002\u5927\u591a\u6570\u6570\u636e\u5e93\u63d0\u4f9b\u4e86\u5185\u5efa\u51fd\u6570\u6765\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u3002\u4e0b\u9762\u6211\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5728\u5e38\u89c1\u7684\u6570\u636e\u5e93\u7cfb\u7edf\uff08\u5982 MySQL\u3001PostgreSQL\u3001Oracle \u7b49\uff09\u4e2d\u53ef\u9760\u5730\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>MySQL \u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>WEEK()<\/code> \u6216 <code>YEARWEEK()<\/code> \u51fd\u6570\u6765\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528 <code>WEEK()<\/code> \u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT WEEK('2025-01-01') AS week_number;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WEEK(date)<\/code> \u8fd4\u56de\u8be5\u65e5\u671f\u6240\u5728\u7684\u5468\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5468\u4e00\u4e3a\u4e00\u5468\u7684\u7b2c\u4e00\u5929\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u5e0c\u671b\u5468\u65e5\u4e3a\u4e00\u5468\u7684\u7b2c\u4e00\u5929\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>WEEK(date, 1)<\/code>\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570 <code>1<\/code> \u8868\u793a\u5468\u65e5\u4e3a\u4e00\u5468\u7684\u7b2c\u4e00\u5929\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528 <code>YEARWEEK()<\/code> \u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>YEARWEEK()<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff0c\u5305\u542b\u5e74\u4efd\u548c\u5468\u6570\uff08\u4f8b\u5982 <code>202501<\/code> \u8868\u793a 2025 \u5e74\u7684\u7b2c 1 \u5468\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT YEARWEEK('2025-01-01') AS year_week;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>YEARWEEK(date)<\/code> \u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u8be5\u65e5\u671f\u6240\u5728\u7684\u5e74\u4efd\u548c\u5468\u6570\u7684\u7ec4\u5408\u3002\u4f8b\u5982 <code>202501<\/code> \u8868\u793a 2025 \u5e74\u7684\u7b2c 1 \u5468\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>PostgreSQL \u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c\u4f7f\u7528 <code>EXTRACT()<\/code> \u51fd\u6570\u6765\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528 <code>EXTRACT()<\/code> \u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT EXTRACT(WEEK FROM DATE '2025-01-01') AS week_number;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>EXTRACT(WEEK FROM date)<\/code> \u8fd4\u56de\u8be5\u65e5\u671f\u6240\u5728\u7684\u5468\u6570\u3002\u8fd4\u56de\u503c\u4e3a\u8be5\u65e5\u671f\u7684\u5468\u6570\uff0c\u57fa\u4e8e ISO 8601 \u6807\u51c6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Oracle \u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Oracle \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>TO_CHAR()<\/code> \u51fd\u6570\u548c <code>IW<\/code> \u683c\u5f0f\u6a21\u578b\u6765\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528 <code>TO_CHAR()<\/code> \u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT TO_CHAR(DATE '2025-01-01', 'IW') AS week_number FROM dual;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>TO_CHAR(date, 'IW')<\/code> \u8fd4\u56de\u8be5\u65e5\u671f\u6240\u5728\u7684\u5468\u6570\uff0c<code>IW<\/code> \u683c\u5f0f\u8868\u793a ISO 8601 \u6807\u51c6\u7684\u5468\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>SQL Server \u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 SQL Server \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>DATEPART()<\/code> \u51fd\u6570\u6765\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528 <code>DATEPART()<\/code> \u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATEPART(WEEK, '2025-01-01') AS week_number;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u89e3\u91ca<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>DATEPART(WEEK, date)<\/code> \u8fd4\u56de\u8be5\u65e5\u671f\u6240\u5728\u7684\u5468\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5468\u65e5\u662f\u6bcf\u5468\u7684\u7b2c\u4e00\u5929\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5904\u7406\u4e0d\u540c\u5468\u5f00\u59cb\u7684\u60c5\u51b5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u5468\u6570\u5b9a\u4e49\u6709\u6240\u4e0d\u540c\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u4e00\u5468\u7684\u5f00\u59cb\u65e5\u671f\u548c\u5468\u6570\u7684\u8ba1\u7b97\u65b9\u5f0f\u3002\u4f8b\u5982\uff0cISO 8601 \u6807\u51c6\u89c4\u5b9a\uff0c\u7b2c\u4e00\u5468\u5fc5\u987b\u5305\u542b\u81f3\u5c11\u56db\u5929\uff0c\u5e76\u4e14\u5468\u4e00\u662f\u6bcf\u5468\u7684\u7b2c\u4e00\u5929\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQL<\/strong>\uff1a\u4f7f\u7528 <code>WEEK(date, 1)<\/code> \u53c2\u6570\u6765\u6307\u5b9a\u5468\u65e5\u4e3a\u6bcf\u5468\u7684\u7b2c\u4e00\u5929\u3002<\/li>\n\n\n\n<li><strong>PostgreSQL<\/strong>\uff1a<code>EXTRACT(WEEK FROM date)<\/code> \u9ed8\u8ba4\u9075\u5faa ISO 8601 \u6807\u51c6\uff0c\u4e0d\u9700\u8981\u989d\u5916\u8bbe\u7f6e\u3002<\/li>\n\n\n\n<li><strong>Oracle<\/strong>\uff1a<code>TO_CHAR(date, 'IW')<\/code> \u4f9d\u636e ISO 8601 \u8ba1\u7b97\u5468\u6570\u3002<\/li>\n\n\n\n<li><strong>SQL Server<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>SET DATEFIRST<\/code> \u6765\u6539\u53d8\u4e00\u5468\u7684\u7b2c\u4e00\u5929\uff0c\u9ed8\u8ba4\u4e3a\u5468\u65e5\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u7684\u65b9\u6cd5\u603b\u7ed3\uff1a<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6570\u636e\u5e93<\/th><th>\u51fd\u6570\/\u65b9\u6cd5<\/th><th>\u793a\u4f8b\u4ee3\u7801<\/th><\/tr><\/thead><tbody><tr><td><strong>MySQL<\/strong><\/td><td><code>WEEK(date)<\/code> \u6216 <code>YEARWEEK(date)<\/code><\/td><td><code>SELECT WEEK('2025-01-01')<\/code><\/td><\/tr><tr><td><strong>PostgreSQL<\/strong><\/td><td><code>EXTRACT(WEEK FROM date)<\/code><\/td><td><code>SELECT EXTRACT(WEEK FROM DATE '2025-01-01')<\/code><\/td><\/tr><tr><td><strong>Oracle<\/strong><\/td><td><code>TO_CHAR(date, 'IW')<\/code><\/td><td><code>SELECT TO_CHAR(DATE '2025-01-01', 'IW') FROM dual<\/code><\/td><\/tr><tr><td><strong>SQL Server<\/strong><\/td><td><code>DATEPART(WEEK, date)<\/code><\/td><td><code>SELECT DATEPART(WEEK, '2025-01-01')<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u5904\u7406\u7a7a\u503c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u7a7a\u503c\uff08<code>NULL<\/code>\uff09\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COALESCE()<\/code> \u6216 <code>IFNULL()<\/code> \u51fd\u6570\u3002\u5047\u5982\u4f60\u60f3\u5728\u67e5\u8be2\u4e2d\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff08\u4f8b\u5982 <code>0<\/code>\uff09\uff0c\u5982\u679c\u5468\u6570\u4e3a <code>NULL<\/code>\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1aMySQL<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COALESCE(WEEK('2025-01-01'), 0) AS week_number;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1aPostgreSQL<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COALESCE(EXTRACT(WEEK FROM DATE '2025-01-01'), 0) AS week_number;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1aOracle<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT NVL(TO_CHAR(DATE '2025-01-01', 'IW'), '0') AS week_number FROM dual;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1aSQL Server<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT ISNULL(DATEPART(WEEK, '2025-01-01'), 0) AS week_number;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4f60\u5728\u9047\u5230\u7a7a\u503c\u65f6\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728 SQL \u67e5\u8be2\u4e2d\u53ef\u9760\u5730\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\uff0c\u5e76\u4e14\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u5dee\u5f02\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQL \u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570\u65f6\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u4f1a\u6709\u4e0d\u540c\u7684\u65b9\u6cd5\u3002\u5927\u591a\u6570\u6570\u636e\u5e93\u63d0\u4f9b\u4e86\u5185\u5efa\u51fd\u6570\u6765\u63d0\u53d6\u65e5\u671f\u7684\u5468\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=643"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":644,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/643\/revisions\/644"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}