V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xiangyuecn
V2EX  ›  程序员

发现多种数据库 group by 对字符串首尾空格的坑死人不偿命规范

  •  1
     
  •   xiangyuecn ·
    xiangyuecn · 2020-11-13 15:10:01 +08:00 · 2300 次点击
    这是一个创建于 1254 天前的主题,其中的信息可能已经有所发展或是发生改变。

    rt 已测试 mysql sqlserver

    "字符串" "字符串 " 会分到同一组

    "字符串" " 字符串" 又不会分到同一组

    这种默认操作站在字符数据存储的角度,和默认不区分大小写一样愚蠢至极,本人观点。

    19 条回复    2020-11-14 12:51:35 +08:00
    lxk11153
        1
    lxk11153  
       2020-11-13 15:31:14 +08:00
    还有这个 feature [doge]
    xiangyuecn
        2
    xiangyuecn  
    OP
       2020-11-13 15:39:34 +08:00
    都是空,表现还不一样:

    \u00a0==' ' mysql✅,sqlserver❌
    \u2000==' ' mysql✅,sqlserver❌
    \u3000==' ' mysql✅,sqlserver✅

    字符串首尾空格,似乎还影响到了 len 、length 、=、!= 、in,太可怕,这种功能却不是另外开一个明确表意的函数来做,黑暗、可怕
    DonaldY
        3
    DonaldY  
       2020-11-13 15:54:01 +08:00
    所以,这种需要 group by 的字段,为什么会出现 空格。
    philchang1995
        4
    philchang1995  
       2020-11-13 15:57:14 +08:00
    @DonaldY 表设计的时候能把后续所有需求都考虑到么?
    telung
        5
    telung  
       2020-11-13 15:57:44 +08:00   ❤️ 2
    发现了这么大的 BUG, 快提给 MYSQL 团队,微软公司啊
    liprais
        6
    liprais  
       2020-11-13 15:57:53 +08:00
    你用的是 varchar 还是 char?
    nikan999
        7
    nikan999  
       2020-11-13 16:00:48 +08:00
    这种情况 是不是考虑数据严格相等比较好,不要去依赖数据库的具体实现
    xiangyuecn
        8
    xiangyuecn  
    OP
       2020-11-13 16:01:44 +08:00
    @DonaldY #3 我也以为是 group by 有影响,但 len 、length 、=、!= 都有影响。并且空格这个定义太宽泛,不同语言实现完全不一致,数据库规范完全是在挖坑。


    js 可以 trim 掉常见但又敲不出的空格,如我 2 楼这 3 个

    java 只能 trim 掉 \u0020 键盘敲出来的这个空格,其他空格 trim 不掉

    其他语言没测试 trim,反正总有个地方会让你跳进去
    xiangyuecn
        9
    xiangyuecn  
    OP
       2020-11-13 16:03:35 +08:00
    @telung #5 别闹😂 胡乱猜测他们妥协的原因也是因为历史原因,别人留下的屎山是不能乱动的
    xiangyuecn
        10
    xiangyuecn  
    OP
       2020-11-13 16:07:12 +08:00
    @nikan999 #7 嗯,这是默认情况下才会出现的坑,如果我要特意去规避,自然有很多办法。但这种默认让普通的建表、查询很受伤
    syozzz
        11
    syozzz  
       2020-11-13 16:41:28 +08:00
    从来还没注意过这个问题==
    CRVV
        12
    CRVV  
       2020-11-13 18:25:42 +08:00
    https://www.postgresql.org/docs/current/datatype-character.html

    网页内搜索 SQL standard
    This somewhat bizarre exception is required by the SQL standard

    通常都用 VARCHAR 的,没这个问题

    或者上 PostgreSQL 全用 TEXT 完事。
    xuanbg
        13
    xuanbg  
       2020-11-13 19:44:16 +08:00
    往后端传的时候都不 trim 掉首尾空格的吗?
    PopRain
        14
    PopRain  
       2020-11-14 06:15:53 +08:00 via Android   ❤️ 1
    保存数据时随便保存,不注意规范数据,回头反过来赖数据库………尾部空格忽略掉的主要原因是最早数据库大部分用固定长度的 Char 类型,忽略尾部空格方便比较。
    xiangyuecn
        15
    xiangyuecn  
    OP
       2020-11-14 08:59:33 +08:00
    @PopRain 典型的以偏概全😂 一棍子打死了有意义的尾部空格
    xiangyuecn
        16
    xiangyuecn  
    OP
       2020-11-14 09:02:20 +08:00
    @PopRain 反倒是早期 char 类型,如果程序存入的不是固定长度,依赖数据库来保证长度一致性,那才是真正的乱存数据吧😂 数据库那调子规范总有那么点为了兼容而去兼容
    PopRain
        17
    PopRain  
       2020-11-14 09:45:11 +08:00
    SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, <Comparison Predicate>, General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations.

    The only exception to this rule is the LIKE predicate. When the right side of a LIKE predicate expression features a value with a trailing space, SQL Server does not pad the two values to the same length before the comparison occurs. Because the purpose of the LIKE predicate, by definition, is to facilitate pattern searches rather than simple string equality tests, this does not violate the section of the ANSI SQL-92 specification mentioned earlier.
    EminemW
        18
    EminemW  
       2020-11-14 11:53:16 +08:00
    为啥数据库会存首尾有空格的字符串,是有什么特殊作用吗
    gulu
        19
    gulu  
       2020-11-14 12:51:35 +08:00 via Android
    明明就是数据库行为不符合预期,各种怪数据自身不规范的。
    就 "ABC" 能做字符串," ABC" 就不配?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2860 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:16 · PVG 21:16 · LAX 06:16 · JFK 09:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.