跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 发现多种数据库 group by 对字符串首尾空格的坑死人不偿命规范
未分類
14 11 月 2020

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

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

資深大佬 : xiangyuecn 0

rt 已测试 mysql sqlserver

“字符串” “字符串 ” 会分到同一组

“字符串” ” 字符串” 又不会分到同一组

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

大佬有話說 (19)

  • 資深大佬 : lxk11153

    还有这个 feature [doge]

  • 主 資深大佬 : xiangyuecn

    都是空,表现还不一样:

    u00a0==’ ‘ mysql✅,sqlserver❌
    u2000==’ ‘ mysql✅,sqlserver❌
    u3000==’ ‘ mysql✅,sqlserver✅

    字符串首尾空格,似乎还影响到了 len 、length 、=、!= 、in,太可怕,这种功能却不是另外开一个明确表意的函数来做,黑暗、可怕

  • 資深大佬 : DonaldY

    所以,这种需要 group by 的字段,为什么会出现 空格。

  • 資深大佬 : philchang1995

    @DonaldY 表设计的时候能把后续所有需求都考虑到么?

  • 資深大佬 : telung

    发现了这么大的 BUG, 快提给 MYSQL 团队,微软公司啊

  • 資深大佬 : liprais

    你用的是 varchar 还是 char?

  • 資深大佬 : nikan999

    这种情况 是不是考虑数据严格相等比较好,不要去依赖数据库的具体实现

  • 主 資深大佬 : xiangyuecn

    @DonaldY #3 我也以为是 group by 有影响,但 len 、length 、=、!= 都有影响。并且空格这个定义太宽泛,不同语言实现完全不一致,数据库规范完全是在挖坑。

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

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

    其他语言没测试 trim,反正总有个地方会让你跳进去

  • 主 資深大佬 : xiangyuecn

    @telung #5 别闹

  • 主 資深大佬 : xiangyuecn

    @nikan999 #7 嗯,这是默认情况下才会出现的坑,如果我要特意去规避,自然有很多办法。但这种默认让普通的建表、查询很受伤

  • 資深大佬 : syozzz

    从来还没注意过这个问题==

  • 資深大佬 : CRVV

    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

    往后端传的时候都不 trim 掉首尾空格的吗?

  • 資深大佬 : PopRain

    保存数据时随便保存,不注意规范数据,回头反过来赖数据库………尾部空格忽略掉的主要原因是最早数据库大部分用固定长度的 Char 类型,忽略尾部空格方便比较。

  • 主 資深大佬 : xiangyuecn

    @PopRain 典型的以偏概全

  • 主 資深大佬 : xiangyuecn

    @PopRain 反倒是早期 char 类型,如果程序存入的不是固定长度,依赖数据库来保证长度一致性,那才是真正的乱存数据吧

  • 資深大佬 : PopRain

    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

    为啥数据库会存首尾有空格的字符串,是有什么特殊作用吗

  • 資深大佬 : gulu

    明明就是数据库行为不符合预期,各种怪数据自身不规范的。
    就 “ABC” 能做字符串,” ABC” 就不配?

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具