208 lines
34 KiB
HTML
208 lines
34 KiB
HTML
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#FFF"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png"><link rel="icon" type="image/ico" sizes="32x32" href="/images/favicon.ico"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link rel="alternate" type="application/rss+xml" title="涛声依旧" href="https://hitoli.com/rss.xml"><link rel="alternate" type="application/atom+xml" title="涛声依旧" href="https://hitoli.com/atom.xml"><link rel="alternate" type="application/json" title="涛声依旧" href="https://hitoli.com/feed.json"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Mulish:300,300italic,400,400italic,700,700italic%7CFredericka%20the%20Great:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20JP:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20SC:300,300italic,400,400italic,700,700italic%7CInconsolata:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext"><link rel="stylesheet" href="/css/app.css?v=0.0.0"><meta name="keywords" content="mysql"><link rel="canonical" href="https://hitoli.com/2023/05/25/MySQL%E4%B8%AD%E7%9A%84any-value-%E5%87%BD%E6%95%B0/"><title>MySQL 中的 any_value () 函数 - 解决问题 - 工作 | ☆∵∴Hito∴∵★ = 涛声依旧 = 天下事有难易乎?为之,则难者亦易矣</title><meta name="generator" content="Hexo 6.3.0"><style>
|
||
#waline {
|
||
position: relative;
|
||
z-index: 10;
|
||
margin-top: 60px !important;
|
||
}
|
||
.post-navigate {
|
||
position: relative;
|
||
z-index: 1;
|
||
margin-bottom: 80px !important;
|
||
}
|
||
/* 评论数为 0 时隐藏整个条目(JS 会给 0 的项加 .no-comment) */
|
||
.meta .item.no-comment {
|
||
display: none;
|
||
}
|
||
/* 侧栏「最新评论」样式:贴合主题卡片风格 */
|
||
.leancloud-recent-comment {
|
||
display: block;
|
||
margin: 0;
|
||
padding: 0;
|
||
list-style: none;
|
||
}
|
||
.leancloud-recent-comment .wl-recent-list {
|
||
margin: 0;
|
||
padding: 0;
|
||
list-style: none;
|
||
}
|
||
.leancloud-recent-comment .wl-recent-item {
|
||
display: block;
|
||
padding: 0.4rem 0;
|
||
border-bottom: 1px dashed var(--color-border, rgba(128,128,128,.25));
|
||
font-size: 0.82rem;
|
||
line-height: 1.5;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
.leancloud-recent-comment .wl-recent-item:last-child {
|
||
border-bottom: none;
|
||
}
|
||
.leancloud-recent-comment .wl-recent-item a {
|
||
font-weight: 600;
|
||
margin-right: 0.25em;
|
||
}
|
||
.leancloud-recent-comment .wl-recent-item p {
|
||
display: inline;
|
||
margin: 0;
|
||
}
|
||
/* 子评论缩进 + 回复标记 */
|
||
.leancloud-recent-comment .wl-recent-child {
|
||
padding-left: 1.4rem;
|
||
border-bottom: none;
|
||
font-size: 0.78rem;
|
||
color: var(--color-secondary, #999);
|
||
}
|
||
.leancloud-recent-comment .wl-reply-tag {
|
||
color: var(--color-primary, #6bc30d);
|
||
margin-right: 0.3em;
|
||
font-size: 0.72rem;
|
||
}
|
||
</style>
|
||
|
||
</head><body itemscope itemtype="http://schema.org/WebPage"><div id="loading"><div class="cat"><div class="body"></div><div class="head"><div class="face"></div></div><div class="foot"><div class="tummy-end"></div><div class="bottom"></div><div class="legs left"></div><div class="legs right"></div></div><div class="paw"><div class="hands left"></div><div class="hands right"></div></div></div></div><div id="container"><header id="header" itemscope itemtype="http://schema.org/WPHeader"><div class="inner"><div id="brand"><div class="pjax"><h1 itemprop="name headline">MySQL 中的 any_value () 函数</h1><div class="meta"><span class="item" title="创建时间:2023-05-25 18:07:00"><span class="icon"><i class="ic i-calendar"></i> </span><span class="text">发表于</span> <time itemprop="dateCreated datePublished" datetime="2023-05-25T18:07:00+08:00">2023-05-25</time> </span><span class="item" title="本文字数"><span class="icon"><i class="ic i-pen"></i> </span><span class="text">本文字数</span> <span>543</span> <span class="text">字</span> </span><span class="item" title="阅读时长"><span class="icon"><i class="ic i-clock"></i> </span><span class="text">阅读时长</span> <span>1 分钟</span> </span><span class="item" title="评论数量"><span class="icon"><i class="ic i-comments"></i> </span><span class="text">评论</span> <span class="waline-comment-count" data-path="/2023/05/25/MySQL中的any-value-函数/"></span> <span class="text">条</span></span></div></div></div><nav id="nav"><div class="inner"><div class="toggle"><div class="lines" aria-label="切换导航栏"><span class="line"></span> <span class="line"></span> <span class="line"></span></div></div><ul class="menu"><li class="item title"><a href="/" rel="start">☆∵∴Hito∴∵★</a></li></ul><ul class="right"><li class="item theme"><i class="ic i-sun"></i></li><li class="item search"><i class="ic i-search"></i></li></ul></div></nav></div><div id="imgs" class="pjax"><ul><li class="item" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?682761"></li><li class="item" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?208863"></li><li class="item" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?518517"></li><li class="item" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?936862"></li><li class="item" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?48911"></li><li class="item" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?255268"></li></ul></div></header><div id="waves"><svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z"/></defs><g class="parallax"><use xlink:href="#gentle-wave" x="48" y="0"/><use xlink:href="#gentle-wave" x="48" y="3"/><use xlink:href="#gentle-wave" x="48" y="5"/><use xlink:href="#gentle-wave" x="48" y="7"/></g></svg></div><main><div class="inner"><div id="main" class="pjax"><div class="article wrap"><div class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList"><i class="ic i-home"></i> <span><a href="/">首页</a></span><i class="ic i-angle-right"></i> <span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="/categories/%E5%B7%A5%E4%BD%9C/" itemprop="item" rel="index" title="分类于 工作"><span itemprop="name">工作</span></a><meta itemprop="position" content="1"></span><i class="ic i-angle-right"></i> <span class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="/categories/%E5%B7%A5%E4%BD%9C/%E8%A7%A3%E5%86%B3%E9%97%AE%E9%A2%98/" itemprop="item" rel="index" title="分类于 解决问题"><span itemprop="name">解决问题</span></a><meta itemprop="position" content="2"></span></div><article itemscope itemtype="http://schema.org/Article" class="post block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="https://hitoli.com/2023/05/25/MySQL%E4%B8%AD%E7%9A%84any-value-%E5%87%BD%E6%95%B0/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/images/avatar.jpg"><meta itemprop="name" content="Hito Li"><meta itemprop="description" content="天下事有难易乎?为之,则难者亦易矣, 天生我材必有用"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="涛声依旧"></span><div class="body md" itemprop="articleBody"><h4 id="问题"><a class="anchor" href="#问题">#</a> 问题</h4><p>业务要求:查询所有省份:<br><img data-src="https://npic.hitoli.com:8443/images/2023/05/25/778f34f5670d.png" alt=""></p><h4 id="方法"><a class="anchor" href="#方法">#</a> 方法</h4><ol><li>distinct 排除重复</li></ol><figure class="highlight sql"><figcaption data-lang="SQL"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">SELECT</span></pre></td></tr><tr><td data-num="2"></td><td><pre> <span class="token keyword">DISTINCT</span><span class="token punctuation">(</span>province_code<span class="token punctuation">)</span><span class="token punctuation">,</span></pre></td></tr><tr><td data-num="3"></td><td><pre> province_name</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">FROM</span></pre></td></tr><tr><td data-num="5"></td><td><pre> t_mip_base_area</pre></td></tr></table></figure><ol start="2"><li>group by 根据身份编码分组</li></ol><figure class="highlight sql"><figcaption data-lang="SQL"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">SELECT</span> </pre></td></tr><tr><td data-num="2"></td><td><pre> province_code<span class="token punctuation">,</span></pre></td></tr><tr><td data-num="3"></td><td><pre> any_value<span class="token punctuation">(</span>province_name<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">FROM</span> t_mip_base_area</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> province_code</pre></td></tr></table></figure><h6 id="若这样写"><a class="anchor" href="#若这样写">#</a> 若这样写</h6><figure class="highlight sql"><figcaption data-lang="SQL"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">SELECT</span> </pre></td></tr><tr><td data-num="2"></td><td><pre> province_code<span class="token punctuation">,</span></pre></td></tr><tr><td data-num="3"></td><td><pre> province_name</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">FROM</span> t_mip_base_area</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> province_code</pre></td></tr></table></figure><p>则会报错<br><img data-src="https://npic.hitoli.com:8443/images/2023/05/25/c57a8e811928.png" alt=""></p><h4 id="总结"><a class="anchor" href="#总结">#</a> 总结:</h4><ol><li><p>MySQL5.7 之后,sql_mode 中 ONLY_FULL_GROUP_BY 模式默认设置为打开状态。</p></li><li><p>ONLY_FULL_GROUP_BY 的语义就是确定 select target list 中的所有列的值都是明确语义,简单的说来,在此模式下,target list 中的值要么是来自于聚合函数(sum、avg、max 等)的结果,要么是来自于 group by list 中的表达式的值</p></li><li><p>MySQL 提供了 any_value () 函数来抑制 ONLY_FULL_GROUP_BY 值被拒绝</p></li><li><p>any_value () 会选择被分到同一组的数据里第一条数据的指定列值作为返回数据</p></li></ol><div class="tags"><a href="/tags/mysql/" rel="tag"><i class="ic i-tag"></i> mysql</a></div></div><footer><div class="meta"><span class="item"><span class="icon"><i class="ic i-calendar-check"></i> </span><span class="text">更新于</span> <time title="修改时间:2025-01-16 15:02:53" itemprop="dateModified" datetime="2025-01-16T15:02:53+08:00">2025-01-16</time></span></div><div class="reward"><button><i class="ic i-heartbeat"></i> 赞赏</button><p>请我喝[茶]~( ̄▽ ̄)~*</p><div id="qr"><div><img data-src="/images/wechatpay.png" alt="Hito Li 微信支付"><p>微信支付</p></div><div><img data-src="/images/alipay.png" alt="Hito Li 支付宝"><p>支付宝</p></div></div></div><div id="copyright"><ul><li class="author"><strong>本文作者: </strong>Hito Li <i class="ic i-at"><em>@</em></i>涛声依旧</li><li class="link"><strong>本文链接:</strong> <a href="https://hitoli.com/2023/05/25/MySQL%E4%B8%AD%E7%9A%84any-value-%E5%87%BD%E6%95%B0/" title="MySQL 中的 any_value () 函数">https://hitoli.com/2023/05/25/MySQL中的any-value-函数/</a></li><li class="license"><strong>版权声明: </strong>本站所有文章除特别声明外,均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpo"><i class="ic i-creative-commons"><em>(CC)</em></i>BY-NC-SA</span> 许可协议。转载请注明出处!</li></ul></div></footer></article></div><div class="post-nav"><div class="item left"><a href="/2023/04/16/%E5%91%BD%E4%BB%A4%E5%A4%A7%E6%9D%82%E7%83%A9/" itemprop="url" rel="prev" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?493811" title="命令大杂烩"><span class="type">上一篇</span> <span class="category"><i class="ic i-flag"></i> 技术分享</span><h3>命令大杂烩</h3></a></div><div class="item right"><a href="/2023/05/25/%E8%A7%A3%E5%86%B3MySQL%E6%8A%A5only-full-group-by%E9%94%99%E8%AF%AF/" itemprop="url" rel="next" data-background-image="https://ndis.hitoli.com:8443/nas-dispatcher/common/downloadImg?510083" title="解决MySQL报only_full_group_by错误"><span class="type">下一篇</span> <span class="category"><i class="ic i-flag"></i> 解决问题</span><h3>解决MySQL报only_full_group_by错误</h3></a></div></div><div class="wrap" id="comments"><div id="waline"></div><link rel="stylesheet" href="https://nblog.hitoli.com:8443/assets/waline.css"><script>(async () => {
|
||
// waline.js 是 ESM 模块,用动态 import 加载(普通 <script src> 会报 export 语法错误)
|
||
const { init, commentCount } = await import('https://nblog.hitoli.com:8443/assets/waline.js');
|
||
|
||
// PJAX 跳转时先清理上一个实例和监听器,防止重复挂载
|
||
if (window.__walineTeardown) { window.__walineTeardown(); }
|
||
|
||
const walineIns = init({
|
||
el: '#waline',
|
||
serverURL: 'https://comment.hitoli.com:8443',
|
||
path: window.location.pathname,
|
||
lang: 'zh-CN',
|
||
placeholder: '欢迎留言讨论~',
|
||
dark: 'html[data-theme="dark"]',
|
||
emoji: [
|
||
'https://nblog.hitoli.com:8443/assets/emojis-1.4.0/weibo/',
|
||
'https://nblog.hitoli.com:8443/assets/emojis-1.4.0/qq/',
|
||
'https://nblog.hitoli.com:8443/assets/emojis-1.4.0/bilibili/'
|
||
]
|
||
})
|
||
|
||
// 文章标题旁 / 页脚的评论数;0 的条目隐藏
|
||
// commentCount 返回 abort 函数而非 Promise,用 MutationObserver 监听数字填入的时机
|
||
const observeCounts = () => {
|
||
document.querySelectorAll('.waline-comment-count').forEach(el => {
|
||
if (el.__countObserved) return;
|
||
el.__countObserved = true;
|
||
const obs = new MutationObserver(() => {
|
||
const item = el.closest('.item');
|
||
if (item) item.classList.toggle('no-comment', el.textContent.trim() === '0' || el.textContent.trim() === '');
|
||
obs.disconnect();
|
||
el.__countObserved = false;
|
||
});
|
||
obs.observe(el, { childList: true, characterData: true, subtree: true });
|
||
});
|
||
};
|
||
observeCounts();
|
||
try { commentCount({ serverURL: 'https://comment.hitoli.com:8443' }); } catch (e) {}
|
||
|
||
const onPjaxSend = function () {
|
||
if (window.__walineIns) { window.__walineIns.destroy(); window.__walineIns = null; }
|
||
}
|
||
document.addEventListener('pjax:send', onPjaxSend)
|
||
window.__walineIns = walineIns
|
||
window.__walineTeardown = function () {
|
||
document.removeEventListener('pjax:send', onPjaxSend)
|
||
if (window.__walineIns) { window.__walineIns.destroy(); window.__walineIns = null; }
|
||
}
|
||
})()</script></div></div><div id="sidebar"><div class="inner"><div class="panels"><div class="inner"><div class="contents panel pjax" data-title="文章目录"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%97%AE%E9%A2%98"><span class="toc-number">1.</span> <span class="toc-text">问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B9%E6%B3%95"><span class="toc-number">2.</span> <span class="toc-text">方法</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%8B%A5%E8%BF%99%E6%A0%B7%E5%86%99"><span class="toc-number">2.0.1.</span> <span class="toc-text">若这样写</span></a></li></ol></li></ol><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">3.</span> <span class="toc-text">总结:</span></a></li></div><div class="related panel pjax" data-title="系列文章"><ul><li><a href="/2022/11/21/mysql%E6%95%B0%E6%8D%AE%E5%BA%93auto-increment%E8%87%AA%E5%A2%9E%E9%95%BF%E4%B8%8D%E5%8F%98%E7%9A%84%E5%A4%84%E7%90%86%E6%96%B9%E6%B3%95/" rel="bookmark" title="mysql数据库auto_increment自增长不变的处理方法">mysql数据库auto_increment自增长不变的处理方法</a></li><li class="active"><a href="/2023/05/25/MySQL%E4%B8%AD%E7%9A%84any-value-%E5%87%BD%E6%95%B0/" rel="bookmark" title="MySQL中的any_value()函数">MySQL中的any_value()函数</a></li><li><a href="/2023/05/25/%E8%A7%A3%E5%86%B3MySQL%E6%8A%A5only-full-group-by%E9%94%99%E8%AF%AF/" rel="bookmark" title="解决MySQL报only_full_group_by错误">解决MySQL报only_full_group_by错误</a></li><li><a href="/2023/05/30/http%E8%AF%B7%E6%B1%82%E4%B9%8BrestTemplate%E9%85%8D%E7%BD%AE%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B4/" rel="bookmark" title="http请求之restTemplate配置超时时间">http请求之restTemplate配置超时时间</a></li><li><a href="/2023/07/08/%E8%A7%A3%E5%86%B3Lombok%E6%8A%A5%E9%94%99/" rel="bookmark" title="解决Lombok报错">解决Lombok报错</a></li><li><a href="/2023/12/02/fastjson%E5%BA%8F%E5%88%97%E5%8C%96%E5%8E%BB%E9%99%A4%E7%A9%BA%E5%AD%97%E7%AC%A6%E4%B8%B2/" rel="bookmark" title="fastjson序列化去除空字符串属性">fastjson序列化去除空字符串属性</a></li><li><a href="/2024/01/03/Centos7%E7%BC%96%E8%AF%91%E5%8D%87%E7%BA%A7nginx/" rel="bookmark" title="Centos7编译升级nginx">Centos7编译升级nginx</a></li><li><a href="/2024/01/03/%E8%A7%A3%E5%86%B3Nginx%E8%AE%BF%E9%97%AE%E8%87%AA%E7%AD%BEssl%E8%AF%81%E4%B9%A6%E6%8A%A5%E4%B8%8D%E5%AE%89%E5%85%A8%E5%91%8A%E8%AD%A6/" rel="bookmark" title="解决Nginx访问自签ssl证书报不安全告警">解决Nginx访问自签ssl证书报不安全告警</a></li><li><a href="/2024/01/19/IntellIJ%E5%8F%AA%E7%BC%96%E8%AF%91%E6%89%93%E5%8C%85%E6%8C%87%E5%AE%9A%E7%9A%84%E6%A8%A1%E5%9D%97/" rel="bookmark" title="IntellIJ只编译打包指定的模块">IntellIJ只编译打包指定的模块</a></li><li><a href="/2024/04/30/Java%E5%90%8E%E7%AB%AF%E7%A6%81%E6%AD%A2%E6%8E%A5%E5%8F%A3%E7%9E%AC%E6%97%B6%E9%87%8D%E5%A4%8D%E8%B0%83%E7%94%A8/" rel="bookmark" title="Java后端禁止接口瞬时重复调用">Java后端禁止接口瞬时重复调用</a></li><li><a href="/2024/05/10/MySQL%E8%A1%A8%E5%88%86%E5%8C%BA/" rel="bookmark" title="MySQL表分区">MySQL表分区</a></li><li><a href="/2024/05/22/%E5%AF%B9XML%E6%A0%BC%E5%BC%8F%E7%9A%84Word%E6%A8%A1%E6%9D%BF%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%A4%84%E7%90%86/" rel="bookmark" title="对XML格式的Word模板格式化处理">对XML格式的Word模板格式化处理</a></li><li><a href="/2024/08/21/%E5%88%A9%E7%94%A8jackson%E5%AF%B9%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE%E5%81%9A%E5%AD%97%E5%85%B8%E8%BD%AC%E6%8D%A2/" rel="bookmark" title="利用jackson对返回数据做字典转换">利用jackson对返回数据做字典转换</a></li></ul></div><div class="overview panel" data-title="站点概览"><div class="author" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="image" itemprop="image" alt="Hito Li" data-src="/images/avatar.jpg"><p class="name" itemprop="name">Hito Li</p><div class="description" itemprop="description">天生我材必有用</div></div><nav class="state"><div class="item posts"><a href="/archives/"><span class="count">65</span> <span class="name">文章</span></a></div><div class="item categories"><a href="/categories/"><span class="count">23</span> <span class="name">分类</span></a></div><div class="item tags"><a href="/tags/"><span class="count">110</span> <span class="name">标签</span></a></div></nav><div class="social"><span class="exturl item weibo" data-url="aHR0cHM6Ly93ZWliby5jb20vdS8xMDYxNDYwNzQ1" title="https://weibo.com/u/1061460745"><i class="ic i-weibo"></i></span> <span class="exturl item douban" data-url="aHR0cHM6Ly93d3cuZG91YmFuLmNvbS9wZW9wbGUvMjU5Mzc2NTY0" title="https://www.douban.com/people/259376564"><i class="ic i-douban"></i></span> <span class="exturl item music" data-url="aHR0cHM6Ly9tdXNpYy4xNjMuY29tLyMvdXNlci9ob21lP2lkPTEwNTQ2NzQ2Mw==" title="https://music.163.com/#/user/home?id=105467463"><i class="ic i-cloud-music"></i></span> <a href="/about/me" title="about/me" class="item about"><i class="ic i-address-card"></i></a></div><ul class="menu"><li class="item"><a href="/" rel="section"><i class="ic i-home"></i>首页</a></li><li class="item dropdown"><a href="javascript:void(0);"><i class="ic i-feather"></i>文章</a><ul class="submenu"><li class="item"><a href="/archives/" rel="section"><i class="ic i-list-alt"></i>归档</a></li><li class="item"><a href="/categories/" rel="section"><i class="ic i-th"></i>分类</a></li><li class="item"><a href="/tags/" rel="section"><i class="ic i-tags"></i>标签</a></li></ul></li><li class="item"><a href="/tools/" rel="section"><i class="ic i-magic"></i>工具</a></li><li class="item"><span class="exturl" data-url="aHR0cHM6Ly93d3cuZm9yZXZlcmJsb2cuY24vZ28uaHRtbA=="><i class="ic i-paper-plane"></i>虫洞</span></li><li class="item"><a href="/about/me" rel="section"><i class="ic i-user"></i>关于</a></li></ul></div></div></div><ul id="quick"><li class="prev pjax"><a href="/2023/04/16/%E5%91%BD%E4%BB%A4%E5%A4%A7%E6%9D%82%E7%83%A9/" rel="prev" title="上一篇"><i class="ic i-chevron-left"></i></a></li><li class="up"><i class="ic i-arrow-up"></i></li><li class="down"><i class="ic i-arrow-down"></i></li><li class="next pjax"><a href="/2023/05/25/%E8%A7%A3%E5%86%B3MySQL%E6%8A%A5only-full-group-by%E9%94%99%E8%AF%AF/" rel="next" title="下一篇"><i class="ic i-chevron-right"></i></a></li><li class="percent"></li></ul></div></div><div class="dimmer"></div></div></main><footer id="footer"><div class="inner"><div class="widgets"><div class="rpost pjax"><h2>随机文章</h2><ul><li class="item"><div class="breadcrumb"><a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/" title="分类于 极空间">极空间</a> <i class="ic i-angle-right"></i> <a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/Docker/" title="分类于 Docker">Docker</a> <i class="ic i-angle-right"></i> <a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/Docker/Hexo/" title="分类于 Hexo">Hexo</a></div><span><a href="/2022/09/17/Hexo-Theme-Shoka-algolia%E6%90%9C%E7%B4%A2%E8%B8%A9%E5%9D%91/" title="Hexo + Theme.Shoka + algolia搜索踩坑">Hexo + Theme.Shoka + algolia搜索踩坑</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/" title="分类于 极空间">极空间</a> <i class="ic i-angle-right"></i> <a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/Docker/" title="分类于 Docker">Docker</a></div><span><a href="/2022/10/21/%E6%9E%81%E7%A9%BA%E9%97%B4Docker%E7%89%88mariadb%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/" title="极空间Docker版mariadb安装与配置">极空间Docker版mariadb安装与配置</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/Linux/" title="分类于 Linux">Linux</a> <i class="ic i-angle-right"></i> <a href="/categories/Linux/Shell/" title="分类于 Shell">Shell</a></div><span><a href="/2023/11/03/Linux%E4%B8%8B%E5%BF%AB%E9%80%9F%E9%83%A8%E7%BD%B2SpringBoot%E9%A1%B9%E7%9B%AE%E7%9A%84%E8%84%9A%E6%9C%AC/" title="Linux下快速部署SpringBoot项目的脚本">Linux下快速部署SpringBoot项目的脚本</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/%E5%B7%A5%E4%BD%9C/" title="分类于 工作">工作</a> <i class="ic i-angle-right"></i> <a href="/categories/%E5%B7%A5%E4%BD%9C/%E8%A7%A3%E5%86%B3%E9%97%AE%E9%A2%98/" title="分类于 解决问题">解决问题</a></div><span><a href="/2024/01/19/IntellIJ%E5%8F%AA%E7%BC%96%E8%AF%91%E6%89%93%E5%8C%85%E6%8C%87%E5%AE%9A%E7%9A%84%E6%A8%A1%E5%9D%97/" title="IntellIJ只编译打包指定的模块">IntellIJ只编译打包指定的模块</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/Windows/" title="分类于 Windows">Windows</a> <i class="ic i-angle-right"></i> <a href="/categories/Windows/%E5%B7%A5%E5%85%B7/" title="分类于 工具">工具</a></div><span><a href="/2022/10/11/svn%E5%BA%93%E8%BD%ACgit%E5%BA%93/" title="svn库转git库">svn库转git库</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/%E5%B7%A5%E4%BD%9C/" title="分类于 工作">工作</a> <i class="ic i-angle-right"></i> <a href="/categories/%E5%B7%A5%E4%BD%9C/%E8%A7%A3%E5%86%B3%E9%97%AE%E9%A2%98/" title="分类于 解决问题">解决问题</a></div><span><a href="/2024/05/10/MySQL%E8%A1%A8%E5%88%86%E5%8C%BA/" title="MySQL表分区">MySQL表分区</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/" title="分类于 极空间">极空间</a> <i class="ic i-angle-right"></i> <a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/Docker/" title="分类于 Docker">Docker</a> <i class="ic i-angle-right"></i> <a href="/categories/%E6%9E%81%E7%A9%BA%E9%97%B4/Docker/Hexo/" title="分类于 Hexo">Hexo</a></div><span><a href="/2023/07/01/shoka%E4%B8%BB%E9%A2%98%E9%80%9F%E5%BA%A6%E4%BC%98%E5%8C%96-%E6%8B%86%E5%88%86jsdelivr/" title="shoka主题速度优化-拆分jsdelivr">shoka主题速度优化-拆分jsdelivr</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/%E7%94%9F%E6%B4%BB/" title="分类于 生活">生活</a> <i class="ic i-angle-right"></i> <a href="/categories/%E7%94%9F%E6%B4%BB/%E6%97%A5%E5%B8%B8%E8%AE%B0%E5%BD%95/" title="分类于 日常记录">日常记录</a></div><span><a href="/2023/10/29/%E7%BB%99%E6%88%91%E7%9A%84%E8%80%81%E7%AC%94%E8%AE%B0%E6%9C%AC%E6%B8%85%E7%90%86%E7%81%B0%E5%B0%98/" title="给我的老笔记本清理灰尘">给我的老笔记本清理灰尘</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/%E5%B7%A5%E4%BD%9C/" title="分类于 工作">工作</a> <i class="ic i-angle-right"></i> <a href="/categories/%E5%B7%A5%E4%BD%9C/Vue/" title="分类于 Vue">Vue</a></div><span><a href="/2022/11/04/%E8%AE%B0%E4%B8%80%E6%AC%A1Vue%E9%A1%B9%E7%9B%AE%E7%9A%84%E9%83%A8%E7%BD%B2/" title="记一次Vue项目的部署">记一次Vue项目的部署</a></span></li><li class="item"><div class="breadcrumb"><a href="/categories/%E5%B7%A5%E4%BD%9C/" title="分类于 工作">工作</a> <i class="ic i-angle-right"></i> <a href="/categories/%E5%B7%A5%E4%BD%9C/%E8%A7%A3%E5%86%B3%E9%97%AE%E9%A2%98/" title="分类于 解决问题">解决问题</a></div><span><a href="/2023/05/25/MySQL%E4%B8%AD%E7%9A%84any-value-%E5%87%BD%E6%95%B0/" title="MySQL中的any_value()函数">MySQL中的any_value()函数</a></span></li></ul></div><div><h2>最新评论</h2><ul class="leancloud-recent-comment"></ul></div></div><div class="status"><div class="copyright">© 2010 – <span itemprop="copyrightYear">2026</span> <span class="with-love"><i class="ic i-sakura rotate"></i> </span><span class="author" itemprop="copyrightHolder">Hito Li @ ☆∵∴Hito∴∵★</span></div><div class="count"><span class="post-meta-item-icon"><i class="ic i-chart-area"></i> </span><span title="站点总字数">136k 字</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="ic i-coffee"></i> </span><span title="站点阅读时长">2:03</span></div><div class="powered-by">基于 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & Theme.<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2FtZWhpbWUvaGV4by10aGVtZS1zaG9rYQ==">Shoka</span></div></div></div></footer></div><script data-config type="text/javascript">var LOCAL={path:"2023/05/25/MySQL中的any-value-函数/",favicon:{show:"(●´3‘●)哎呀呀",hide:"(´Д`)真糟糕!"},search:{placeholder:"文章搜索",empty:"关于 「 ${query} 」,什么也没搜到",stats:"${time} ms 内找到 ${hits} 条结果"},fancybox:!0,copyright:'复制成功,转载请遵守 <i class="ic i-creative-commons"></i>BY-NC-SA 协议。',ignores:[function(e){return e.includes("#")},function(e){return new RegExp(LOCAL.path+"$").test(e)}]}</script><script src="https://nblog.hitoli.com:8443/assets/polyfill.js"></script><script src="https://nblog.hitoli.com:8443/assets/pace.min.js"></script><script src="https://nblog.hitoli.com:8443/assets/pjax.min.js"></script><script src="https://nblog.hitoli.com:8443/assets/fetch.umd.js"></script><script src="https://nblog.hitoli.com:8443/assets/anime.min.js"></script><script src="https://nblog.hitoli.com:8443/assets/algoliasearch-lite.umd.min.js"></script><script src="https://nblog.hitoli.com:8443/assets/instantsearch.production.min.js"></script><script src="https://nblog.hitoli.com:8443/assets/lozad.min.js"></script><script src="https://nblog.hitoli.com:8443/assets/quicklink.umd.min.js"></script><script src="https://nblog.hitoli.com:8443/assets/jquery.min.js,jquery.fancybox.min.js,jquery.justifiedGallery.min.js" async></script><script src="https://nblog.hitoli.com:8443/assets/copy-tex.min.js" async></script><script src="https://nblog.hitoli.com:8443/assets/frappe-charts.min.iife.js"></script><script src="https://nblog.hitoli.com:8443/assets/autosize.min.js,xss.min.js,ua-parser.min.js,tex-svg.js,marked.min.js"></script><script src="/js/app.js?v=0.0.0"></script><script>
|
||
// 侧栏「最新评论」:挂在 footer 的 .leancloud-recent-comment 容器(PJAX 不替换 footer,全局渲染一次即可)
|
||
// 动态 import 加载 ESM 版 waline.js;普通 <script src> 会报 export 语法错误
|
||
(async () => {
|
||
try {
|
||
const { commentCount } = await import('https://nblog.hitoli.com:8443/assets/waline.js');
|
||
// 评论数为 0 / 空时隐藏整个条目(commentCount 返回的是 abort 函数而非 Promise,
|
||
// 所以用 MutationObserver 监听它把数字填进 DOM 的时机来处理)
|
||
const observeCounts = () => {
|
||
document.querySelectorAll('.waline-comment-count').forEach(el => {
|
||
if (el.__countObserved) return;
|
||
el.__countObserved = true;
|
||
const obs = new MutationObserver(() => {
|
||
const item = el.closest('.item');
|
||
if (item) item.classList.toggle('no-comment', el.textContent.trim() === '0' || el.textContent.trim() === '');
|
||
obs.disconnect();
|
||
el.__countObserved = false;
|
||
});
|
||
obs.observe(el, { childList: true, characterData: true, subtree: true });
|
||
});
|
||
};
|
||
// 填文章列表/头部的评论数。PJAX 跳转后由 comment.njk 里重新跑
|
||
const fillCounts = () => {
|
||
observeCounts();
|
||
try { commentCount({ serverURL: 'https://comment.hitoli.com:8443' }); } catch (e) {}
|
||
};
|
||
fillCounts();
|
||
// PJAX 跳转后重新填一次(footer 不被替换,但 #main 里的计数元素是新的)
|
||
document.addEventListener('pjax:success', fillCounts);
|
||
// 最新评论列表(footer 里的容器,全局一次即可)
|
||
// 注意:不用 waline.js 的 RecentComments —— 自托管版本有 bug(getRecentComment
|
||
// 忘了解 .data,返回 {errno,...} 而不是数组,渲染判断 e.length>0 永远失败)
|
||
// 这里直接 fetch 接口自己渲染,还顺带解决了评论点击跳转(url 为空时按 path 跳)
|
||
const loadRecent = (attempt) => {
|
||
if (attempt > 3) return;
|
||
const el = document.querySelector('.leancloud-recent-comment');
|
||
if (!el) return;
|
||
const opts = (typeof AbortSignal !== 'undefined' && AbortSignal.timeout) ? { signal: AbortSignal.timeout(15000) } : {};
|
||
fetch('https://comment.hitoli.com:8443/api/comment?type=recent&count=8&lang=zh-CN', opts)
|
||
.then(r => { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); })
|
||
.then(res => {
|
||
const list = (res && res.data) || [];
|
||
if (!list.length) { setTimeout(() => loadRecent(attempt + 1), 3000 * attempt); return; }
|
||
// 组装层级:pid 指向父评论 objectId。父评论不在本批数据里时按顶级处理
|
||
const byId = {};
|
||
list.forEach(c => { byId[c.objectId] = c; });
|
||
const nickOf = (id) => (byId[id] && byId[id].nick) || '';
|
||
const esc = (s) => String(s || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||
const itemHtml = (c, indent) => {
|
||
const tmp = document.createElement('div');
|
||
tmp.innerHTML = c.comment || '';
|
||
// 去掉服务端已加的 "@昵称 ," 前缀(渲染成缩进层级后冗余)
|
||
let text = (tmp.textContent || '').replace(/^\s*@\S+?\s*,\s*/, '').trim().replace(/\s+/g, ' ').slice(0, 60);
|
||
const replyTag = indent ? '<span class="wl-reply-tag">↳ 回复 ' + esc(nickOf(c.pid)) + '</span>' : '';
|
||
const link = c.url || c.path || '';
|
||
return '<li class="wl-recent-item' + (indent ? ' wl-recent-child' : '') + '">'
|
||
+ replyTag
|
||
+ '<a href="' + link + '">' + esc(c.nick || '匿名') + '</a>:' + esc(text)
|
||
+ '</li>';
|
||
};
|
||
// 子评论紧跟在父评论后面输出(保持数据原有时间序)
|
||
const rendered = {};
|
||
const html = [];
|
||
list.forEach(c => {
|
||
if (rendered[c.objectId]) return;
|
||
if (!c.pid || !byId[c.pid]) {
|
||
html.push(itemHtml(c, false));
|
||
rendered[c.objectId] = true;
|
||
// 输出它的子孙(可能连续多层,循环找)
|
||
let changed = true;
|
||
while (changed) {
|
||
changed = false;
|
||
list.forEach(s => {
|
||
if (!rendered[s.objectId] && s.pid && rendered[s.pid]) {
|
||
html.push(itemHtml(s, true));
|
||
rendered[s.objectId] = true;
|
||
changed = true;
|
||
}
|
||
});
|
||
}
|
||
}
|
||
});
|
||
// 兜底:pid 指向的父评论比自己晚出现的数据(乱序),补渲染
|
||
list.forEach(c => { if (!rendered[c.objectId]) html.push(itemHtml(c, true)); });
|
||
el.innerHTML = '<ul class="wl-recent-list">' + html.join('') + '</ul>';
|
||
})
|
||
.catch(() => {
|
||
setTimeout(() => loadRecent(attempt + 1), 3000 * attempt);
|
||
});
|
||
};
|
||
loadRecent(1);
|
||
} catch (e) {
|
||
console.error('Waline inject failed:', e)
|
||
}
|
||
})()
|
||
</script>
|
||
|
||
</body></html> |