{"id":2386,"date":"2025-03-04T20:51:36","date_gmt":"2025-03-04T12:51:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2386"},"modified":"2025-03-04T20:51:36","modified_gmt":"2025-03-04T12:51:36","slug":"sqlite-%e6%97%a5%e6%9c%9f-%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/04\/sqlite-%e6%97%a5%e6%9c%9f-%e6%97%b6%e9%97%b4\/","title":{"rendered":"SQLite\u00a0\u65e5\u671f &amp; \u65f6\u95f4"},"content":{"rendered":"\n<p>\u5728 SQLite \u4e2d\uff0c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5904\u7406\u5e76\u4e0d\u50cf\u5728\u5176\u4ed6\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u90a3\u6837\u63d0\u4f9b\u4e13\u95e8\u7684\u6570\u636e\u7c7b\u578b\u3002SQLite \u6ca1\u6709\u5185\u7f6e\u7684 <code>DATE<\/code> \u6216 <code>TIME<\/code> \u7c7b\u578b\uff0c\u800c\u662f\u901a\u8fc7\u6587\u672c\u3001\u6574\u6570\u6216\u5b9e\u6570\u7c7b\u578b\u6765\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u3002SQLite \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b58\u50a8\u3001\u5904\u7406\u548c\u67e5\u8be2\u65e5\u671f\u4e0e\u65f6\u95f4\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5b58\u50a8\u65b9\u5f0f<\/strong><\/h3>\n\n\n\n<p>SQLite \u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u6709\u4e09\u79cd\u5e38\u89c1\u7684\u683c\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6587\u672c\uff08TEXT\uff09<\/strong>\uff1a\u4ee5 <code>YYYY-MM-DD HH:MM:SS<\/code> \u683c\u5f0f\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/li>\n\n\n\n<li><strong>\u6574\u6570\uff08INTEGER\uff09<\/strong>\uff1a\u5b58\u50a8\u81ea\u7eaa\u5143\uff081970\u5e741\u67081\u65e500:00:00 UTC\uff09\u4ee5\u6765\u7684\u79d2\u6570\uff08Unix \u65f6\u95f4\u6233\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u6570\uff08REAL\uff09<\/strong>\uff1a\u5b58\u50a8\u81ea\u7eaa\u5143\u4ee5\u6765\u7684\u5929\u6570\uff08\u5305\u62ec\u5c0f\u6570\u90e8\u5206\uff0c\u7cbe\u786e\u5230\u5929\u548c\u65f6\u95f4\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>SQLite \u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p>SQLite \u63d0\u4f9b\u4e86\u591a\u4e2a\u5185\u7f6e\u7684\u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong><code>DATE()<\/code><\/strong><\/h4>\n\n\n\n<p><code>DATE()<\/code> \u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u5f53\u524d\u65e5\u671f\uff0c\u6216\u8005\u6839\u636e\u6307\u5b9a\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u8ba1\u7b97\u65e5\u671f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATE('now');  -- \u8fd4\u56de\u5f53\u524d\u65e5\u671f (YYYY-MM-DD)<\/code><\/pre>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f20\u5165\u5176\u4ed6\u65e5\u671f\u8fdb\u884c\u8ba1\u7b97\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATE('2025-03-04', '+1 day');  -- \u8fd4\u56de '2025-03-05'\nSELECT DATE('2025-03-04', '-1 day');  -- \u8fd4\u56de '2025-03-03'\nSELECT DATE('2025-03-04', '+2 years');  -- \u8fd4\u56de '2027-03-04'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong><code>TIME()<\/code><\/strong><\/h4>\n\n\n\n<p><code>TIME()<\/code> \u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\uff0c\u6216\u8005\u8ba1\u7b97\u7ed9\u5b9a\u65f6\u95f4\u7684\u65f6\u95f4\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT TIME('now');  -- \u8fd4\u56de\u5f53\u524d\u65f6\u95f4 (HH:MM:SS)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong><code>DATETIME()<\/code><\/strong><\/h4>\n\n\n\n<p><code>DATETIME()<\/code> \u51fd\u6570\u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u6216\u8005\u6839\u636e\u6307\u5b9a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATETIME('now');  -- \u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4 (YYYY-MM-DD HH:MM:SS)<\/code><\/pre>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u7528\u6765\u52a0\u51cf\u65f6\u95f4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATETIME('2025-03-04 12:00:00', '+1 day');  -- \u8fd4\u56de '2025-03-05 12:00:00'\nSELECT DATETIME('2025-03-04 12:00:00', '-1 hour');  -- \u8fd4\u56de '2025-03-04 11:00:00'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.4 <strong><code>JULIANDAY()<\/code><\/strong><\/h4>\n\n\n\n<p><code>JULIANDAY()<\/code> \u51fd\u6570\u8fd4\u56de\u81ea\u516c\u5143\u524d4713\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u5112\u7565\u65e5\u6570\uff0c\u8868\u793a\u4e3a\u4e00\u4e2a\u5b9e\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JULIANDAY('now');  -- \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u5112\u7565\u65e5\u6570<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.5 <strong><code>STRFTIME()<\/code><\/strong><\/h4>\n\n\n\n<p><code>STRFTIME()<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u5316\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u8fd4\u56de\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT STRFTIME('%Y-%m-%d %H:%M:%S', 'now');  -- \u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\nSELECT STRFTIME('%A, %B %d, %Y', 'now');  -- \u8fd4\u56de\u7c7b\u4f3c 'Monday, March 04, 2025'<\/code><\/pre>\n\n\n\n<p><code>STRFTIME<\/code> \u652f\u6301\u591a\u79cd\u683c\u5f0f\u5b57\u7b26\uff0c\u7528\u4e8e\u8f93\u51fa\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%Y<\/code>\uff1a\u56db\u4f4d\u5e74\u4efd<\/li>\n\n\n\n<li><code>%m<\/code>\uff1a\u6708\u4efd\uff0801-12\uff09<\/li>\n\n\n\n<li><code>%d<\/code>\uff1a\u65e5\u671f\uff0801-31\uff09<\/li>\n\n\n\n<li><code>%H<\/code>\uff1a\u5c0f\u65f6\uff0800-23\uff09<\/li>\n\n\n\n<li><code>%M<\/code>\uff1a\u5206\u949f\uff0800-59\uff09<\/li>\n\n\n\n<li><code>%S<\/code>\uff1a\u79d2\u6570\uff0800-59\uff09<\/li>\n\n\n\n<li><code>%A<\/code>\uff1a\u661f\u671f\u51e0\u7684\u5168\u79f0\uff08\u4f8b\u5982\uff1aMonday\uff09<\/li>\n\n\n\n<li><code>%B<\/code>\uff1a\u6708\u4efd\u7684\u5168\u79f0\uff08\u4f8b\u5982\uff1aMarch\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u8ba1\u7b97\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h3>\n\n\n\n<p>SQLite \u652f\u6301\u901a\u8fc7 <code>+<\/code> \u6216 <code>-<\/code> \u64cd\u4f5c\u7b26\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATE('2025-03-04', '+5 days');  -- \u8fd4\u56de '2025-03-09'\nSELECT DATE('2025-03-04', '-1 year');  -- \u8fd4\u56de '2024-03-04'\nSELECT DATETIME('now', '+3 hours');  -- \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u52a0\u4e0a3\u5c0f\u65f6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u65e5\u671f\u548c\u65f6\u95f4\u4e0e\u5f53\u524d\u65f6\u95f4\u7684\u6bd4\u8f83<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 SQLite \u4e2d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u67e5\u8be2\u4eca\u5929\u548c\u7279\u5b9a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u6216\u8005\u6839\u636e\u65e5\u671f\u7b5b\u9009\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u524d\u540e\u7684\u8bb0\u5f55\nSELECT * FROM orders WHERE order_date &gt; DATE('now');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u4ece Unix \u65f6\u95f4\u6233\u4e2d\u63d0\u53d6\u65e5\u671f<\/strong><\/h3>\n\n\n\n<p>SQLite \u53ef\u4ee5\u4ece Unix \u65f6\u95f4\u6233\u4e2d\u63d0\u53d6\u65e5\u671f\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b58\u50a8 Unix \u65f6\u95f4\u6233\u7684\u5217 <code>timestamp<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>DATETIME()<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATETIME(timestamp, 'unixepoch') FROM logs;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684 <code>'unixepoch'<\/code> \u662f\u6307\u5c06\u5b58\u50a8\u7684 Unix \u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u65e5\u671f\u65f6\u95f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5904\u7406\u65f6\u533a<\/strong><\/h3>\n\n\n\n<p>SQLite \u7684\u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570\u672c\u8eab\u4e0d\u652f\u6301\u65f6\u533a\u8f6c\u6362\u3002\u6240\u6709\u7684\u65e5\u671f\u548c\u65f6\u95f4\u503c\u901a\u5e38\u662f\u4ee5 UTC \u65f6\u95f4\u5904\u7406\u7684\u3002\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u65f6\u533a\uff0c\u53ef\u4ee5\u5728\u5e94\u7528\u5c42\u9762\u4e0a\u505a\u76f8\u5e94\u7684\u8f6c\u6362\uff0c\u6216\u8005\u4f7f\u7528 UTC \u65f6\u95f4\u5e76\u5728\u5c55\u793a\u65f6\u8fdb\u884c\u672c\u5730\u65f6\u533a\u8f6c\u6362\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u5c0f\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SQLite \u4e0d\u63d0\u4f9b\u4e13\u95e8\u7684 <code>DATE<\/code> \u6216 <code>TIME<\/code> \u6570\u636e\u7c7b\u578b\uff0c\u800c\u662f\u901a\u8fc7\u6587\u672c\u3001\u6574\u6570\u548c\u5b9e\u6570\u7c7b\u578b\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/li>\n\n\n\n<li>\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\uff08\u5982 <code>DATE()<\/code>\u3001<code>TIME()<\/code>\u3001<code>DATETIME()<\/code>\u3001<code>STRFTIME()<\/code> \u7b49\uff09\u6765\u5904\u7406\u548c\u8ba1\u7b97\u65e5\u671f\u65f6\u95f4\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>+<\/code> \u6216 <code>-<\/code> \u6765\u5bf9\u65e5\u671f\u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\u3002<\/li>\n\n\n\n<li><code>STRFTIME()<\/code> \u662f\u6700\u5e38\u7528\u7684\u51fd\u6570\uff0c\u80fd\u591f\u7075\u6d3b\u5730\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5185\u7f6e\u7684\u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570\uff0cSQLite \u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u6ee1\u8db3\u5404\u79cd\u65e5\u671f\u65f6\u95f4\u8ba1\u7b97\u548c\u67e5\u8be2\u9700\u6c42\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6269\u5c55\u7684\u4e3b\u9898\u548c\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u65e5\u671f\u4e0e\u65f6\u95f4\u7684\u7c7b\u578b\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<p>\u5728 SQLite \u4e2d\uff0c\u65e5\u671f\u548c\u65f6\u95f4\u88ab\u5b58\u50a8\u4e3a\u6587\u672c\u3001\u6574\u6570\u6216\u5b9e\u6570\uff0c\u5982\u4f55\u5728\u8fd9\u4e9b\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u662f\u4f7f\u7528 SQLite \u5904\u7406\u65e5\u671f\u65f6\u95f4\u65f6\u9700\u8981\u4e86\u89e3\u7684\u91cd\u8981\u5185\u5bb9\u3002SQLite \u63d0\u4f9b\u4e86\u51e0\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.1 <strong>\u4ece Unix \u65f6\u95f4\u6233\u8f6c\u6362<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u4f60\u6709 Unix \u65f6\u95f4\u6233\uff08\u81ea 1970 \u5e74 1 \u6708 1 \u65e5\u4ee5\u6765\u7684\u79d2\u6570\uff09\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATETIME(1609459200, 'unixepoch');  -- \u8f93\u51fa '2021-01-01 00:00:00'<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u67e5\u8be2\u5c06\u7ed9\u5b9a\u7684 Unix \u65f6\u95f4\u6233 <code>1609459200<\/code> \u8f6c\u6362\u4e3a\u6807\u51c6\u7684\u65e5\u671f\u65f6\u95f4\u683c\u5f0f <code>YYYY-MM-DD HH:MM:SS<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.2 <strong>\u5c06\u65e5\u671f\u65f6\u95f4\u8f6c\u6362\u4e3a Unix \u65f6\u95f4\u6233<\/strong><\/h4>\n\n\n\n<p>\u540c\u6837\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06\u65e5\u671f\u65f6\u95f4\u8f6c\u6362\u4e3a Unix \u65f6\u95f4\u6233\uff0c\u4f7f\u7528 <code>strftime<\/code> \u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT STRFTIME('%s', '2025-03-04 12:00:00');  -- \u8f93\u51fa '1741382400'<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684 <code>%s<\/code> \u8fd4\u56de\u81ea\u7eaa\u5143\uff081970\u5e741\u67081\u65e5\uff09\u4ee5\u6765\u7684\u79d2\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u65e5\u671f\u548c\u65f6\u95f4\u7684\u504f\u79fb\u91cf<\/strong><\/h3>\n\n\n\n<p>SQLite \u652f\u6301\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u52a0\u51cf\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4e00\u4e9b\u5e38\u89c1\u7684\u65e5\u671f\u8ba1\u7b97\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c <code>'YYYY-MM-DD'<\/code> \u7684\u683c\u5f0f\u548c\u504f\u79fb\u91cf\u6765\u8c03\u6574\u65e5\u671f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.1 <strong>\u5411\u65e5\u671f\u6dfb\u52a0\u6216\u51cf\u53bb\u65f6\u95f4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATE('2025-03-04', '+1 month');  -- \u8fd4\u56de '2025-04-04'\nSELECT DATE('2025-03-04', '-2 weeks');  -- \u8fd4\u56de '2025-02-18'\nSELECT DATETIME('2025-03-04 12:00:00', '+2 hours');  -- \u8fd4\u56de '2025-03-04 14:00:00'<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u67e5\u8be2\u5c55\u793a\u4e86\u5982\u4f55\u5728\u65e5\u671f\u548c\u65f6\u95f4\u4e0a\u52a0\u4e0a\u6216\u51cf\u53bb\u65f6\u95f4\u5355\u4f4d\uff0c\u5982\u6708\u3001\u5468\u3001\u5929\u3001\u5c0f\u65f6\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9.2 <strong>\u4f7f\u7528\u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570\u8ba1\u7b97\u5dee\u5f02<\/strong><\/h4>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT JULIANDAY('2025-03-04') - JULIANDAY('2024-03-04');  -- \u8fd4\u56de '366' (\u95f0\u5e74)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c<code>JULIANDAY<\/code> \u51fd\u6570\u8fd4\u56de\u4ece\u516c\u5143\u524d4713\u5e741\u67081\u65e5\u8d77\u7684\u65e5\u6570\uff0c\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u4f60\u53ef\u4ee5\u5f97\u5230\u76f8\u5dee\u7684\u5929\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>\u65f6\u533a\u5904\u7406<\/strong><\/h3>\n\n\n\n<p>SQLite \u672c\u8eab\u5e76\u4e0d\u652f\u6301\u65f6\u533a\uff0c\u56e0\u6b64\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5b58\u50a8\u901a\u5e38\u662f\u57fa\u4e8e UTC\uff08\u534f\u8c03\u4e16\u754c\u65f6\uff09\u3002\u5728\u8fdb\u884c\u65f6\u533a\u8f6c\u6362\u65f6\uff0c\u901a\u5e38\u662f\u5728\u5e94\u7528\u5c42\u9762\u8fdb\u884c\u5904\u7406\u3002\u5982\u679c\u4f60\u60f3\u5728 SQLite \u4e2d\u5904\u7406\u65f6\u533a\uff0c\u4f60\u53ef\u4ee5\u5b58\u50a8\u65e5\u671f\u548c\u65f6\u95f4\u7684 UTC \u8868\u793a\uff0c\u5e76\u5728\u67e5\u8be2\u65f6\u6839\u636e\u9700\u8981\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u77e5\u9053 UTC \u65f6\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>DATETIME()<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATETIME('2025-03-04 12:00:00', 'localtime');  -- \u5c06 UTC \u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684 <code>'localtime'<\/code> \u9009\u9879\u4f1a\u5c06 UTC \u65f6\u95f4\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\uff0c\u524d\u63d0\u662f SQLite \u8fd0\u884c\u73af\u5883\u6709\u6b63\u786e\u7684\u672c\u5730\u65f6\u533a\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. <strong>\u65e5\u671f\u683c\u5f0f\u5316<\/strong><\/h3>\n\n\n\n<p>SQLite \u5141\u8bb8\u4f7f\u7528 <code>STRFTIME()<\/code> \u51fd\u6570\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u683c\u5f0f\u5316\u7b26\u53f7\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%Y<\/code>\uff1a\u56db\u4f4d\u5e74\u4efd\uff08\u4f8b\u5982\uff1a2025\uff09<\/li>\n\n\n\n<li><code>%m<\/code>\uff1a\u6708\u4efd\uff0801-12\uff09<\/li>\n\n\n\n<li><code>%d<\/code>\uff1a\u65e5\u671f\uff0801-31\uff09<\/li>\n\n\n\n<li><code>%H<\/code>\uff1a\u5c0f\u65f6\uff0800-23\uff09<\/li>\n\n\n\n<li><code>%M<\/code>\uff1a\u5206\u949f\uff0800-59\uff09<\/li>\n\n\n\n<li><code>%S<\/code>\uff1a\u79d2\u6570\uff0800-59\uff09<\/li>\n\n\n\n<li><code>%A<\/code>\uff1a\u661f\u671f\u51e0\u7684\u5168\u79f0\uff08\u4f8b\u5982\uff1aMonday\uff09<\/li>\n\n\n\n<li><code>%B<\/code>\uff1a\u6708\u4efd\u7684\u5168\u79f0\uff08\u4f8b\u5982\uff1aMarch\uff09<\/li>\n\n\n\n<li><code>%w<\/code>\uff1a\u661f\u671f\u51e0\uff080=Sunday, 6=Saturday\uff09<\/li>\n\n\n\n<li><code>%s<\/code>\uff1aUnix \u65f6\u95f4\u6233\uff08\u79d2\u6570\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT STRFTIME('%A, %B %d, %Y %H:%M:%S', 'now');  -- \u8fd4\u56de\u5f53\u524d\u65e5\u671f\u65f6\u95f4\uff0c\u5982 'Monday, March 04, 2025 12:30:45'\nSELECT STRFTIME('%Y-%m-%d', '2025-03-04');  -- \u8fd4\u56de '2025-03-04'\nSELECT STRFTIME('%H:%M:%S', 'now');  -- \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\uff0c\u5982 '12:30:45'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">12. <strong>\u5904\u7406\u95f0\u5e74\u548c\u6708\u5dee<\/strong><\/h3>\n\n\n\n<p>SQLite \u4f1a\u81ea\u52a8\u5904\u7406\u95f0\u5e74\u548c\u6708\u4efd\u7684\u4e0d\u540c\u5929\u6570\u3002\u4f8b\u5982\uff0c2 \u6708\u6709 28 \u6216 29 \u5929\uff0c\u6708\u4efd\u7684\u5929\u6570\u4f1a\u968f\u7740\u5e74\u4efd\u7684\u4e0d\u540c\u800c\u53d8\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATE('2024-02-29', '+1 month');  -- 2024\u5e74\u662f\u95f0\u5e74\uff0c\u8fd4\u56de '2024-03-29'\nSELECT DATE('2025-02-28', '+1 month');  -- 2025\u5e74\u4e0d\u662f\u95f0\u5e74\uff0c\u8fd4\u56de '2025-03-28'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">13. <strong>\u65e5\u671f\u65f6\u95f4\u7684\u6392\u5e8f<\/strong><\/h3>\n\n\n\n<p>SQLite \u53ef\u4ee5\u76f4\u63a5\u5bf9\u65e5\u671f\u65f6\u95f4\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u67e5\u8be2\u4e2d\uff0c\u65e5\u671f\u548c\u65f6\u95f4\u4f1a\u6309\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5e8f\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u5217\u7684\u6570\u636e\u7c7b\u578b\u548c\u67e5\u8be2\u8bed\u53e5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM events ORDER BY event_date ASC;  -- \u6309\u7167\u65e5\u671f\u5347\u5e8f\u6392\u5e8f\nSELECT * FROM events ORDER BY event_date DESC;  -- \u6309\u7167\u65e5\u671f\u964d\u5e8f\u6392\u5e8f<\/code><\/pre>\n\n\n\n<p>\u5982\u679c <code>event_date<\/code> \u5217\u5b58\u50a8\u7684\u662f\u6587\u672c\u7c7b\u578b\u7684\u65e5\u671f\uff08\u5982 <code>'YYYY-MM-DD'<\/code> \u683c\u5f0f\uff09\uff0cSQLite \u4f1a\u6309\u5b57\u5178\u987a\u5e8f\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff0c\u56e0\u4e3a\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u5b57\u5178\u987a\u5e8f\u548c\u65e5\u671f\u7684\u65f6\u95f4\u987a\u5e8f\u662f\u76f8\u540c\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">14. <strong>\u7ed3\u5408 <code>WHERE<\/code> \u5b50\u53e5\u4f7f\u7528\u65e5\u671f<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728 <code>WHERE<\/code> \u5b50\u53e5\u4e2d\u4f7f\u7528\u65e5\u671f\u548c\u65f6\u95f4\u51fd\u6570\u6765\u7b5b\u9009\u7279\u5b9a\u7684\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM orders WHERE order_date &gt; DATE('now', '-7 days');  -- \u6700\u8fd17\u5929\u7684\u8ba2\u5355\nSELECT * FROM events WHERE event_time &lt; DATETIME('now');  -- \u6240\u6709\u8fc7\u53bb\u7684\u4e8b\u4ef6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">15. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>SQLite \u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u51fd\u6570\u548c\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>DATE()<\/code>\u3001<code>TIME()<\/code>\u3001<code>DATETIME()<\/code> \u7b49\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u3001\u65f6\u95f4\u6216\u65e5\u671f\u65f6\u95f4\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>STRFTIME()<\/code> \u51fd\u6570\u53ef\u4ee5\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\uff0c\u7075\u6d3b\u5730\u6839\u636e\u9700\u6c42\u5448\u73b0\u4e0d\u540c\u7684\u683c\u5f0f\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>+<\/code> \u548c <code>-<\/code> \u64cd\u4f5c\u7b26\uff0cSQLite \u652f\u6301\u5bf9\u65e5\u671f\u65f6\u95f4\u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\uff0c\u5e2e\u52a9\u8ba1\u7b97\u65e5\u671f\u5dee\u5f02\u6216\u8c03\u6574\u65e5\u671f\u3002<\/li>\n\n\n\n<li>SQLite \u9ed8\u8ba4\u5904\u7406\u65e5\u671f\u65f6\u95f4\u4e3a UTC\uff0c\u56e0\u6b64\u65f6\u533a\u8f6c\u6362\u901a\u5e38\u9700\u8981\u5728\u5e94\u7528\u5c42\u9762\u8fdb\u884c\u3002<\/li>\n\n\n\n<li>\u5728\u67e5\u8be2\u65f6\uff0cSQLite \u652f\u6301\u65e5\u671f\u6392\u5e8f\u3001\u7b5b\u9009\u548c\u683c\u5f0f\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u638c\u63e1\u8fd9\u4e9b SQLite \u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u6570\u636e\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQLite \u4e2d\uff0c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5904\u7406\u5e76\u4e0d\u50cf\u5728\u5176\u4ed6\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u90a3\u6837\u63d0\u4f9b\u4e13\u95e8\u7684\u6570\u636e\u7c7b\u578b\u3002SQLite \u6ca1\u6709\u5185\u7f6e\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2386","post","type-post","status-publish","format-standard","hentry","category-sqlite"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2386","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=2386"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2386\/revisions"}],"predecessor-version":[{"id":2387,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2386\/revisions\/2387"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}