{"id":10021074,"date":"2025-11-11T22:10:47","date_gmt":"2025-11-11T14:10:47","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021074"},"modified":"2025-11-11T22:10:47","modified_gmt":"2025-11-11T14:10:47","slug":"%e6%95%88%e8%83%bd%e5%b7%a5%e5%85%b7%ef%bc%88%e4%b9%9d%ef%bc%89%e4%b9%8b%e7%bc%96%e5%86%99nodejs%e8%84%9a%e6%9c%ac%e4%bd%bf%e7%94%a8get-video-duration%e6%89%b9%e9%87%8f%e8%af%bb%e5%8f%96%e8%a7%86","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021074","title":{"rendered":"\u6548\u80fd\u5de5\u5177\uff08\u4e5d\uff09\u4e4b\u7f16\u5199nodejs\u811a\u672c\u4f7f\u7528get-video-duration\u6279\u91cf\u8bfb\u53d6\u89c6\u9891\u65f6\u957f\uff0c\u5e76\u751f\u6210sql\u8bed\u53e5\u4fee\u590d\u6570\u636e\u5e93\u8868\u5b57\u6bb5\u503c"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u770b\u6587\uff0c\u770b\u7684\u662f\u4e00\u79cd\u601d\u8def\uff0c\u5e0c\u671b\u7b14\u8005\u7684\u6587\u7ae0\u80fd\u7ed9\u8bf8\u4f4d\u5e26\u6765\u4e00\u4e9b\u7075\u611f\u601d\u8def\u263a\ufe0f\u263a\ufe0f\u263a\ufe0f<\/p>\n<h3>\u573a\u666f\u6982\u8ff0<\/h3>\n<p>\u5b9e\u9645\u573a\u666f\u63cf\u8ff0\u6709\u4e9b\u5197\u4f59\uff0c\u7279\u62bd\u8c61\u51fa\u6765\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<p>\u516c\u53f8\u5e73\u53f0\u4e0a\u6709\u4e00\u4e2a\u89c6\u9891\u6587\u4ef6\u5217\u8868\uff0c\u5217\u8868\u6709\u8fd1\u5343\u6761\u6570\u636e\uff0c\u5bf9\u5e94\u63a5\u53e3\u8fd4\u56de\u7684\u5c31\u662f\u4e00\u4e2a\u6570\u7ec4\uff0clength\u5c06\u8fd11000\uff0c\u5982\uff1a<\/p>\n<pre><code class=\"js\">let tableData = [\n    { id: 1, name: '\u89c6\u98911', duration: 364, url: 'https:\/\/abc.com.cn\/files\/1.mp4' },\n \u00a0  { id: 2, name: '\u89c6\u98912', duration: 210, url: 'https:\/\/abc.com.cn\/files\/2.mp4' },\n \u00a0 \u00a0...\n \u00a0  { id: 999, name: '\u89c6\u9891999', duration: 540, url: 'https:\/\/abc.com.cn\/files\/999.mp4' },\n]<\/code><\/pre>\n<blockquote><p>\u5176\u4e2d\uff0cname\u662f\u89c6\u9891\u540d\u5b57\uff0cduration\u662f\u89c6\u9891\u65f6\u957f\uff08\u79d2\uff09\uff0curl\u662f\u89c6\u9891\u9759\u6001\u8d44\u6e90\u53ef\u8bbf\u95ee\u5730\u5740<\/p><\/blockquote>\n<ul>\n<li>\u73b0\u5728\u7684\u95ee\u9898\u662f\uff0c\u56e0\u4e3a\u4e00\u4e9b\u65e0\u8bed\u7684\u539f\u56e0\uff0c\u8fd9999\u6761\u6570\u636e\u4e2d\uff0c\u5b58\u5728\u90e8\u5206\u6570\u636e\u5b58\u50a8\u7684duration\u5b57\u6bb5\u7684\u503c\u4e0d\u5bf9\uff0c\u548c\u5b9e\u9645\u89c6\u9891\u7684\u65f6\u957f\u5bf9\u4e0d\u4e0a\uff0c\u6216\u591a\u6216\u5c11<\/li>\n<li>\u6bd4\u5982\u7b2c\u4e00\u6761\u6570\u636e\uff0c\u89c6\u98911\u7684\u5b9e\u9645\u65f6\u957f\u662f464\u79d2\uff0c\u4f46duration\u5b57\u6bb5\u503c\u5b58\u7684\u662f364\u79d2<\/li>\n<li>\u73b0\u5728\u9700\u8981\u5c06\u5176\u4fee\u590d\u6210\u6b63\u786e\u7684\u65f6\u957f\uff0c\u4f46\u662f\u56e0\u4e3a\u6570\u636e\u91cf\u592a\u591a\uff0c\u8fd1\u5343\u6761\u6570\u636e\uff0c\u4eba\u5de5\u4e00\u6761\u4e00\u6761\u6838\u5bf9\uff0c\u6548\u7387\u5341\u5206\u4f4e\u4e0b\uff0c\u800c\u4e14\u4eba\u5de5\u6838\u5bf9\u5bb9\u6613\u51fa\u9519<\/li>\n<\/ul>\n<p>\u56e0\u6b64\u7b14\u8005\u5199\u4e86\u4e00\u4e2anodejs\u811a\u672c\uff0c\u6279\u91cf\u6267\u884c\u6548\u7387\u9ad8\uff0c\u8d28\u91cf\u6709\u4fdd\u969c<\/p>\n<h3>\u89e3\u51b3\u65b9\u6848\u601d\u8def<\/h3>\n<ul>\n<li>\u9996\u5148\uff0c\u5faa\u73aftableData\u5f97\u5230\u6570\u7ec4\u7684\u6bcf\u4e00\u9879<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528<a href=\"https:\/\/link.segmentfault.com\/?enc=z86%2Fu%2FZf2GgkogT4kGG54g%3D%3D.z8N279J6oXy7f1rZWUCAlQzYi1AG1kE4AdsaKTKTKnVhfzUax16vitZTixQh0x4auBoPXaDk61nzA%2BZLA9RNmg%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">get-video-duration<\/a>\u8fd9\u4e2a\u5305\uff0c\u8bfb\u53d6\u6bcf\u4e00\u9879\u7684url\u5bf9\u5e94\u7684\u89c6\u9891\u8d44\u6e90<\/li>\n<li>\u5f97\u5230\u5bf9\u5e94\u89c6\u9891\u771f\u6b63\u7684\u65f6\u957f\uff0c\u6bd4\u5982\u53eb\u505atrueDurationNum\uff0c\u548c\u5f53\u524d\u7684duration\u5bf9\u6bd4\u4e00\u4e0b<\/li>\n<li>\u82e5\u76f8\u540c\uff0c\u5219\u4ee3\u8868\u65f6\u957f\u6ca1\u95ee\u9898\uff1b\u82e5\u4e0d\u540c\uff0c\u5219\u5355\u72ec\u62ce\u51fa\u6765\u4e22\u5230notEqualArr\u6570\u7ec4\u91cc<\/li>\n<li>\u6700\u540e\uff0c\u518d\u628anotEqualArr\u7edf\u4e00\u5faa\u73af\u5904\u7406<\/li>\n<li>\u6216\u8c03\u7528\u4fee\u6539\u63a5\u53e3\uff0c\u6279\u91cf\u8bf7\u6c42\u4fee\u6539\u6210\u6b63\u786e\u7684\u89c6\u9891\u65f6\u957f<\/li>\n<li>\u6216\u8005\u5199\u4e2a\u51fd\u6570\u5c06\u5176\u8f6c\u6210sql\u8bed\u53e5\uff0c\u76f4\u63a5\u4e00\u6761\u547d\u4ee4\u6267\u884c\u89e3\u51b3\u95ee\u9898<\/li>\n<\/ul>\n<h3>get-video-duration\u5305\u4ecb\u7ecd<\/h3>\n<p>get-video-duration \u662fgithub\u4e0a\u7684\u62e5\u6709140\u4e2aStar\u7684\u5c0f\u4f17\u5305\uff0c\u4f20\u7ed9\u5b83\u4e00\u4e2a\u89c6\u9891\u7684url\uff0c\u5b83\u5c31\u53ef\u4ee5\u8fd4\u56de\u6b64\u89c6\u9891\u5bf9\u5e94\u7684\u65f6\u957f\u4fe1\u606f\uff0c\u5982\u4e0b<\/p>\n<pre><code class=\"js\">const { getVideoDurationInSeconds } = require('get-video-duration')\nconst duration = await getVideoDurationInSeconds(url)\nconsole.log('\u89c6\u9891\u65f6\u957f\/\u79d2', duration)<\/code><\/pre>\n<p>\u652f\u6301mp4\u3001mov\u3001\u591a\u79cd\u89c6\u9891\u683c\u5f0f\uff0c\u5176\u5e95\u5c42\u4f9d\u8d56FFmpeg\u7684ffprobe\u5957\u4ef6\uff0c\u80fd\u591f\u5206\u6790\u97f3\u89c6\u9891\uff08\u6bd4\u5982\u5206\u8fa8\u7387\u3001\u7f16\u7801\u683c\u5f0f\u3001\u65f6\u957f\uff09\u5176\u4f9d\u8d56\u7b80\u7ea6\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"json\">{\n \u00a0&quot;name&quot;: &quot;get-video-duration&quot;,\n \u00a0&quot;description&quot;: &quot;Get the duration of a video file&quot;,\n \u00a0&quot;version&quot;: &quot;4.1.0&quot;,\n \u00a0&quot;author&quot;: &quot;Llu\u00eds Ulzurrun de Asanza S\u00e0ez &lt;me@llu.is&gt; (http:\/\/llu.is)&quot;,\n \u00a0&quot;license&quot;: &quot;MIT&quot;,\n \u00a0&quot;repository&quot;: &quot;caffco\/get-video-duration&quot;,\n \u00a0&quot;main&quot;: &quot;dist\/commonjs\/index.js&quot;,\n \u00a0&quot;module&quot;: &quot;dist\/es6\/index.js&quot;,\n \u00a0&quot;dependencies&quot;: {\n \u00a0 \u00a0&quot;@ffprobe-installer\/ffprobe&quot;: &quot;^2.1.2&quot;,\n \u00a0 \u00a0&quot;execa&quot;: &quot;^5.0.0&quot;,\n \u00a0 \u00a0&quot;is-stream&quot;: &quot;^2.0.0&quot;\n  },\n \u00a0......\n}<\/code><\/pre>\n<blockquote><p>\u5b9e\u9645\u4e0a\uff0c\u5305\u7684\u4f5c\u8005\u9664\u4e86\u6709\u8fd9\u4e2a\u83b7\u53d6\u89c6\u9891\u65f6\u957f\u7684\u5de5\u5177\u5305\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u83b7\u53d6\u97f3\u9891\u65f6\u957f\u7684\u5305\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=sdUlVNs%2FWYLNp%2FNz1zDYsg%3D%3D.M%2BLvfBB%2FpvOVBU%2BuHnuN8mcpEJjY0Y6o%2Bqh4dGhHXxhY%2BES1ppcfkgBixndz%2F1fyS5ynvU62u8dUh1sVB9yF0g%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">get-audio-duration<\/a><\/p><\/blockquote>\n<h2>\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<p>\u56e0\u4e3a\u8981\u5b89\u88c5\u5305\uff0c\u6240\u4ee5\u8981<code>npm inti -y<\/code>\u7b80\u5355\u521b\u5efa\u4e00\u4e2anode\u5de5\u7a0b<\/p>\n<p>\u800c\u540e\u5b89\u88c5\u5bf9\u5e94\u4f9d\u8d56<code>npm i get-video-duration<\/code><\/p>\n<pre><code class=\"json\">{\n \u00a0&quot;name&quot;: &quot;video-duration-check&quot;,\n \u00a0&quot;version&quot;: &quot;1.0.0&quot;,\n \u00a0&quot;main&quot;: &quot;app.js&quot;,\n \u00a0&quot;scripts&quot;: {\n \u00a0 \u00a0&quot;test&quot;: &quot;echo &quot;Error: no test specified&quot; &amp;&amp; exit 1&quot;\n  },\n \u00a0&quot;keywords&quot;: [],\n \u00a0&quot;author&quot;: &quot;&quot;,\n \u00a0&quot;license&quot;: &quot;ISC&quot;,\n \u00a0&quot;description&quot;: &quot;&quot;,\n \u00a0&quot;dependencies&quot;: {\n \u00a0 \u00a0&quot;get-video-duration&quot;: &quot;^4.1.0&quot;\n  }\n}<\/code><\/pre>\n<h3>\u68c0\u67e5\u662f\u5426\u6709\u4e0d\u76f8\u7b49\u7684\u89c6\u9891\u65f6\u957f\u9879<\/h3>\n<pre><code class=\"js\">\/\/ app.js\nconst { getVideoDurationInSeconds } = require('get-video-duration')\nconst fs = require('fs')\n\/\/ \u7ed9\u5230\u4e00\u4e2aurl\uff0c\u8fd4\u56de\u89c6\u9891\u65f6\u957f\nasync function getVideoDuration(url) {\n \u00a0 \u00a0try {\n \u00a0 \u00a0 \u00a0 \u00a0const duration = await getVideoDurationInSeconds(url)\n \u00a0 \u00a0 \u00a0 \u00a0return Math.floor(duration) \/\/ \u5411\u4e0b\u53d6\u6574\u65f6\u957f\uff0c\u7cbe\u5ea6\u5230\u79d2\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ return duration \/\/ \u7cbe\u51c6\u65f6\u957f\uff0c\u7cbe\u5ea6\u5230\u6beb\u79d2\n \u00a0  } catch (error) {\n \u00a0 \u00a0 \u00a0 \u00a0throw error\n \u00a0  }\n}\n\/\/ \u63a5\u53e3\u8fd4\u56de\u6570\u636e\u7684\u793a\u4f8b\nconst tableData = [\n \u00a0  { id: 1, name: '\u89c6\u98911', duration: 364, url: 'https:\/\/abc.com.cn\/files\/1.mp4' },\n \u00a0  { id: 2, name: '\u89c6\u98912', duration: 210, url: 'https:\/\/abc.com.cn\/files\/2.mp4' },\n \u00a0  { id: 999, name: '\u89c6\u9891999', duration: 540, url: 'https:\/\/abc.com.cn\/files\/999.mp4' },\n]\nasync function main() {\n \u00a0 \u00a0\/\/ \u5b58\u50a8\u4e0d\u5339\u914d\u7684\u6570\u636e\n \u00a0 \u00a0const notEqualArr = []\n \u00a0 \u00a0\/\/ \u904d\u5386tableData\uff0c\u8bfb\u53d6\u6bcf\u4e2a\u89c6\u9891\u7684\u65f6\u957f\n \u00a0 \u00a0for (const item of tableData) {\n \u00a0 \u00a0 \u00a0 \u00a0try {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0const trueDurationNum = await getVideoDuration(item.url) \/\/ \u8bfb\u53d6\u65f6\u957f\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0if (item.duration !== trueDurationNum) { \/\/ \u5982\u679c\u65f6\u957f\u4e0d\u5339\u914d\uff0c\u5219\u5b58\u5165notEqualArr\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u628a\u771f\u5b9e\u7684\u65f6\u957f\u4e5f\u5b58\u5230item\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\uff0c\u8bf7\u6c42\u4fee\u6539\u63a5\u53e3\uff0c\u6216\u8005\u751f\u6210sql\u8bed\u53e5\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0item.trueDurationNum = trueDurationNum\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0notEqualArr.push(item)\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  }\n \u00a0 \u00a0 \u00a0  } catch (error) {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u4e5f\u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406\u9519\u8bef\uff0c\u6bd4\u5982\u65b0\u5efa\u4e00\u4e2aerrorArr\uff0c\u628a\u9519\u8bef\u4fe1\u606f\u5b58\u8fdb\u53bb\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.error(error)\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n \u00a0 \u00a0console.log(`\u5171\u6709${tableData.length}\u4e2a\u89c6\u9891\uff0c\u5176\u4e2d\u4e0d\u5339\u914d${notEqualArr.length}\u4e2a`)\n \u00a0 \u00a0fs.writeFileSync('notEqualArr.json', JSON.stringify(notEqualArr, null, 2))\n \u00a0 \u00a0console.log('notEqualArr.json \u6587\u4ef6\u5df2\u4fdd\u5b58')\n}\nmain()<\/code><\/pre>\n<p>\u7ecf\u8fc7\u8fd9\u6837\u4e00\u6ce2\u64cd\u4f5c\uff0c\u5c31\u80fd\u5f97\u5230duration\u4e0d\u5bf9\u7684notEqualArr\u6570\u636e\u9879\u4e86\uff0c\u5047\u8bbe\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">const notEqualArr = [\n \u00a0  { id: 3, name: '\u89c6\u98913', duration: 364, trueDurationNum: 3333, url: 'https:\/\/abc.com.cn\/files\/3.mp4' },\n \u00a0  { id: 4, name: '\u89c6\u98914', duration: 210, trueDurationNum: 4444, url: 'https:\/\/abc.com.cn\/files\/4.mp4' },\n]<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6709\u4e86\u4e0d\u76f8\u7b49\u7684\u6570\u636e\u4e86\uff0c\u53ef\u4ee5\u9009\u62e9\u4e24\u79cd\u65b9\u6848<\/p>\n<ul>\n<li>\u4e00\u79cd\u662f\u5faa\u73afnotEqualArr\u6570\u7ec4\uff0c\u7136\u540e\u901a\u8fc7\u7f16\u8f91\u63a5\u53e3\u4fee\u6539duration\u7684\u503c\u4e3atrueDurationNum\u7684\u503c<\/li>\n<li>\u7b2c\u4e8c\u79cd\u662f\u62fc\u63a5\u6210\u5bf9\u5e94\u7684sql\uff0c\u76f4\u63a5\u901a\u8fc7DBeaver\u6216\u8005Navicat \u76f4\u63a5\u4e00\u6761sql\u641e\u5b9a<\/li>\n<\/ul>\n<h3>\u65b9\u5f0f\u4e00\uff1a\u901a\u8fc7\u7f16\u8f91\u63a5\u53e3\u4fee\u6539<\/h3>\n<p>\u5982\u4e0b\u793a\u4f8b\u601d\u8def\u4ee3\u7801<\/p>\n<pre><code class=\"js\">\/\/ \u7f16\u8f91\u63a5\u53e3\nconst baseUrl = 'https:\/\/abc.com.cn\/api\/editVideo'\n\/\/ \u767b\u5f55\u7cfb\u7edf\u540e\uff0c\u590d\u5236\u4e00\u4efd\u8bf7\u6c42\u5934\u7684 Authorization\nconst Authorization = 'Bearer eyJhbGci...'\n\/\/ \u5b58\u50a8\u4e0d\u5339\u914d\u7684\u6570\u636e\nconst notEqualArr = [\n \u00a0  { id: 3, name: '\u89c6\u98913', duration: 364, trueDurationNum: 3333, url: 'https:\/\/abc.com.cn\/files\/3.mp4' },\n \u00a0  { id: 4, name: '\u89c6\u98914', duration: 210, trueDurationNum: 4444, url: 'https:\/\/abc.com.cn\/files\/4.mp4' },\n]\n\/\/ \u5faa\u73af\u53d1\u8bf7\u6c42\u4fee\u6539\u5bf9\u5e94duration\u5b57\u6bb5\u7684\u503c\u4e3atrueDurationNum\nasync function main() {\n \u00a0 \u00a0console.time('main')\n \u00a0 \u00a0for (const item of notEqualArr) {\n \u00a0 \u00a0 \u00a0 \u00a0try {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0const res = await fetch(baseUrl, {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0headers: {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'Authorization': Authorization,\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0'Content-Type': 'application\/json'\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  },\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0method: 'POST',\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0body: JSON.stringify({\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0id: item.id,\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0duration: item.trueDurationNum\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  })\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  })\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0const { data } = await res.json()\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.log(`\u66f4\u65b0\u6210\u529f - ID: ${item.id}, \u65b0\u7684\u65f6\u957f: ${item.trueDurationNum}`)\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.log(data)\n \u00a0 \u00a0 \u00a0  } catch (error) {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0console.error(`\u66f4\u65b0\u5931\u8d25 - ID: ${item.id}, \u9519\u8bef: ${error.message}`)\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n \u00a0 \u00a0console.timeEnd('main')\n}\nmain()<\/code><\/pre>\n<h3>\u65b9\u5f0f\u4e8c\uff1a\u901a\u8fc7sql\u4fee\u6539<\/h3>\n<p>\u56de\u987e\u4e00\u4e0b<\/p>\n<ul>\n<li>\u5047\u8bbe\uff0c\u6211\u8981\u6279\u91cf\u4fee\u6539student\u8868\u91cc\u9762\u7684<\/li>\n<li>id\u4e3a2\u7684\u90a3\u6761\u6570\u636e\uff0c\u5c06\u5176\u5e74\u9f84\u6539\u4e3a22<\/li>\n<li>id\u4e3a3\u7684\u90a3\u6761\u6570\u636e\uff0c\u5c06\u5176\u5e74\u9f84\u6539\u4e3a33\uff0c\u5199\u6cd5\u5982\u4e0b<\/li>\n<\/ul>\n<pre><code class=\"sql\">UPDATE student\nSET age = CASE id\n \u00a0 \u00a0WHEN 2 THEN 22\n \u00a0 \u00a0WHEN 3 THEN 33\nEND\nWHERE id IN (2, 3);<\/code><\/pre>\n<p>\u5408\u5e76\u6210\u4e3a\u4e00\u884c\u8bed\u6cd5<\/p>\n<pre><code class=\"sql\">UPDATE student SET age = CASE id WHEN 2 THEN 22 WHEN 3 THEN 33 END WHERE id IN (2, 3);<\/code><\/pre>\n<p>\u5bf9\u5e94\u4e0a\u8ff0\u4fee\u6539duration\u7684\u5199\u6cd5\u5c31\u662f\uff08\u5047\u8bbe\u8868\u662fvideo_table\uff09<\/p>\n<pre><code class=\"js\">const notEqualArr = [\n \u00a0 \u00a0{ id: 3, name: '\u89c6\u98913', duration: 364, trueDurationNum: 3333, url: 'https:\/\/abc.com.cn\/files\/3.mp4' },\n \u00a0 \u00a0{ id: 4, name: '\u89c6\u98914', duration: 210, trueDurationNum: 4444, url: 'https:\/\/abc.com.cn\/files\/4.mp4' },\n]\n\/\/ \u6362\u884c\nUPDATE video_table\nSET duration = CASE id\n \u00a0 \u00a0WHEN 3 THEN 3333\n \u00a0 \u00a0WHEN 4 THEN 4444\nEND\nWHERE id IN (3, 4);\n\n\/\/ \u4e0d\u6362\u884c\uff0c\u4e00\u884c\u8bed\u53e5\u5c31\u662f\nUPDATE video_table SET duration = CASE id WHEN 3 THEN 3333 WHEN 4 THEN 4444 END WHERE id IN (3, 4);<\/code><\/pre>\n<p>\u6240\u4ee5\uff0c\u53ea\u9700\u8981\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u6570\u7ec4notEqualArr\u8f6c\u6210\u5bf9\u5e94\u7684\u5355\u6761sql\u8bed\u53e5\u5373\u53ef<\/p>\n<p>\u8f6c\u6362\u51fd\u6570\u5199\u6cd5\u5982\u4e0b<\/p>\n<pre><code class=\"js\">const notEqualArr = [\n \u00a0  { id: 3, name: '\u89c6\u98913', duration: 364, trueDurationNum: 3333, url: 'https:\/\/abc.com.cn\/files\/3.mp4' },\n \u00a0  { id: 4, name: '\u89c6\u98914', duration: 210, trueDurationNum: 4444, url: 'https:\/\/abc.com.cn\/files\/4.mp4' },\n]\n\/**\n * \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6279\u91cf\u66f4\u65b0 SQL \u8bed\u53e5\n * @param {Array} arr - \u5305\u542b id \u548c trueDurationNum \u7684\u6570\u7ec4\n * @param {string} tableName - \u8868\u540d\uff0c\u9ed8\u8ba4\u4e3a 'video_table'\n * @returns {string} \u751f\u6210\u7684 SQL \u8bed\u53e5\n *\/\nfunction generateSql(arr, tableName = 'video_table') {\n \u00a0 \u00a0\/\/ \u6784\u5efa CASE WHEN \u8bed\u53e5\n \u00a0 \u00a0const caseStatements = arr.map(item =&gt;\n \u00a0 \u00a0 \u00a0 \u00a0`WHEN ${item.id} THEN ${item.trueDurationNum}`\n \u00a0  ).join(' ');\n \u00a0 \u00a0console.log('caseStatements----&gt;', caseStatements); \/\/ WHEN 3 THEN 3333 WHEN 4 THEN 4444\n \u00a0 \u00a0\/\/ \u6784\u5efa IN \u6761\u4ef6\n \u00a0 \u00a0const ids = arr.map(item =&gt; item.id).join(',');\n \u00a0 \u00a0console.log('ids----&gt;', ids); \/\/ 3,4\n \u00a0 \u00a0\/\/ \u751f\u6210\u5b8c\u6574\u7684 SQL \u8bed\u53e5\n \u00a0 \u00a0const sql = `UPDATE ${tableName} SET duration = CASE id ${caseStatements} END WHERE id IN (${ids});`;\n \u00a0 \u00a0return sql;\n}\nfunction main() {\n \u00a0 \u00a0const sql = generateSql(notEqualArr);\n \u00a0 \u00a0return sql;\n}\nconsole.log(main());\n\/\/ UPDATE video_table SET duration = CASE id WHEN 3 THEN 3333 WHEN 4 THEN 4444 END WHERE id IN (3,4);<\/code><\/pre>\n<p><strong>\u6700\u540e\uff0c\u7b14\u8005\u91c7\u53d6\u65b9\u5f0f\u4e8c\uff0c\u76f4\u63a5sql\u6267\u884c\u7684\u65b9\u6848\uff08\u5148\u5728\u6d4b\u8bd5\u73af\u5883\u8bd5\u4e00\u4e0b\uff09\u6700\u7ec8\u5feb\u901f\u89e3\u51b3\u4e86\u8fd9\u4e2a\u89c6\u9891\u65f6\u957f\u4e0d\u5bf9\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684notEqualArr\u7b14\u8005\u662f\u76f4\u63a5\u5199\u5230\u4ee3\u7801\u91cc\u9762\uff0c\u4e0d\u662f\u5c06\u5176\u4e22\u5230\u4e00\u4e2a.json\u6587\u4ef6\u91cc\u9762\uff0c\u518dconst notEqualArr = require(&#8216;.\/notEqualArr.json&#8217;)\u5f15\u5165\u8fdb\u6765\uff0c\u8fd9\u6837\u5c31\u80fd\u907f\u514drequire\u7f13\u5b58\u673a\u5236\uff0c\u53ef\u53c2\u89c1\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=%2FLsRsv4u86xORVDS4cnF9w%3D%3D.rynGJjKhrAo1%2FWIDr3u3jzdY528lGn37bT3CiXbxGDg%2FeRnT3ih61Dv8Iq2SK8dTGt5uL3sIcwoVIYzUHchwUw%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u8bf7\u4e0d\u8981\u4f7f\u7528require\u5f15\u5165\u5355\u4e2a\u6587\u4ef6<\/a><\/p>\n<blockquote><p>A good memory is better than a bad pen. Record it down&#8230;\u263a\ufe0f\u263a\ufe0f\u263a\ufe0f<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047387869\">https:\/\/segmentfault.com\/a\/1190000047387869<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u770b\u6587\uff0c\u770b\u7684\u662f\u4e00\u79cd\u601d\u8def\uff0c\u5e0c\u671b\u7b14\u8005\u7684\u6587\u7ae0\u80fd\u7ed9\u8bf8\u4f4d\u5e26\u6765\u4e00\u4e9b\u7075\u611f\u601d\u8def\u263a\ufe0f\u263a\ufe0f\u263a\ufe0f \u573a\u666f\u6982\u8ff0 \u5b9e\u9645\u573a\u666f\u63cf\u8ff0\u6709\u4e9b\u5197\u4f59 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021074","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021074"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021074\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}