You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL查询优化:将Em Dash和En Dash替换为单破折号生成Slug

解决MySQL生成Slug时特殊破折号(Em Dash/En Dash)的问题

原查询在处理包含En Dash(–)或Em Dash(—)的字符串时,无法正确转换这些特殊符号,导致生成的slug出现多余连字符(例如arrival-–-free-time-or-optional-activities)。以下是优化后的查询,可解决该问题:

SELECT *, 
LOWER(
  REPLACE(
    REPLACE(
      REPLACE(
        REPLACE(
          REPLACE(
            REPLACE(
              REPLACE(
                REPLACE(
                  REPLACE(
                    REPLACE(
                      REPLACE(
                        REPLACE(
                          REPLACE(
                            REPLACE(
                              REPLACE(
                                REPLACE(
                                  TRIM(name), 
                                  ':', ''), 
                                ')', ''), 
                              '(', ''), 
                            ',', ''), 
                          '\\', ''), 
                        '\/', ''), 
                      '\\"', ''), 
                    '?', ''), 
                  '\'', ''), 
                '&', ''), 
              '!', ''), 
            '.', ''), 
          ' ', '-'), 
        '–', '-'), -- 替换En Dash为普通连字符
        '—', '-'), -- 替换Em Dash为普通连字符
      '--', '-'), 
    '--', '-'
  ) AS slug 
FROM program;

关键修改说明

  1. 新增特殊破折号替换:在替换空格为连字符后,添加对En Dash('–')和Em Dash('—')的替换,将它们统一转为普通连字符'-'
  2. 重复连字符清理:保留原有的多次REPLACE('--', '-')操作,确保所有连续的连字符被合并为单个连字符

优化后生成的slug会符合预期:arrival-free-time-or-optional-activities

内容的提问来源于stack exchange,提问作者Ered

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 00:37:52