[英]Comparison with trailing spaces in MySQL
此 SQL 查询:
select c1 from table where c1='';
返回 MySQL 中具有c1=' '
(一个空格)的行。
这是故意的还是错误?
编辑:请在此处检查 SQL Fiddle 链接, SELECT
查询中的空格数无关紧要。
这一切都在文档中说明。 我在这里引用了重要的观点。 但我建议阅读完整的文档
VARCHAR 值在存储时不会被填充。 根据标准 SQL,在存储和检索值时保留尾随空格。
另一方面,CHAR 值在存储时会被填充,但在检索时会忽略尾随空格。
所有 MySQL 归类都是 PADSPACE 类型。 这意味着比较 MySQL 中的所有 CHAR、VARCHAR 和 TEXT 值,而不考虑任何尾随空格。 此上下文中的“比较”不包括 LIKE 模式匹配运算符,对于其尾随空格很重要。
说明:使用比较运算符 ('=') 比较字符串时,会忽略Trailing spaces
。 但是尾随空格对于LIKE
( pattern matching operator
)很重要
如果您的列来自CHAR类型而不是 VARCHAR,那么这是正确的。 在 CHAR-Fields 上将忽略比较时的尾随空白! 所以
field = ''
field = ' '
是相同的。
此行为符合 ANSI SQL-92 标准。 任何符合此标准的数据库都将表现出相同的行为。 引用:
3) The comparison of two character strings is determined as fol- lows: a) If the length in characters of X is not equal to the length in characters of Y, then the shorter string is effectively replaced, for the purposes of comparison, with a copy of itself that has been extended to the length of the longer string by concatenation on the right of one or more pad char- acters, where the pad character is chosen based on CS. If CS has the NO PAD attribute, then the pad character is an implementation-dependent character different from any char- acter in the character set of X and Y that collates less than any string under CS. Otherwise, the pad character is a <space>. b) The result of the comparison of X and Y is given by the col- lating sequence CS.
因此,根据这些规范'abc' = 'abc '
和'' = ' '
评估为真(但'' = '\\t'
为假)。
如果c1
是CHAR(1)
,那么这是正确的,因为CHAR
列是固定宽度的,如有必要,将用空格填充。
因此,即使您将''
放入CHAR(1)
字段中,您也会在SELECT
得到' '
。 此外,过滤空字符串将产生' '
。
请接受马丁史密斯的回答,因为他在我面前给出了正确的提示。
此外,根据
MySQL 文档,将字符串与
=
进行比较时会忽略尾随空格,因此如果您的
c1
列仅包含空格(或在您的情况下包含空格),即使您过滤
WHERE c1 = ''
也会返回它:
特别是,尾随空格很重要,这对于使用 = 运算符执行的 CHAR 或 VARCHAR 比较而言并非如此
mysql> SELECT 'a' = 'a ', 'a' LIKE 'a '; +------------+---------------+ | 'a' = 'a ' | 'a' LIKE 'a ' | +------------+---------------+ | 1 | 0 | +------------+---------------+ 1 row in set (0.00 sec)
尝试这个 -
Select case when c1 = '' then ' ' else c1 end from table ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.