Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 将字符串与一个之前有空格而另一个没有空格的字符串进行比较_Mysql_Sql - Fatal编程技术网

Mysql 将字符串与一个之前有空格而另一个没有空格的字符串进行比较

Mysql 将字符串与一个之前有空格而另一个没有空格的字符串进行比较,mysql,sql,Mysql,Sql,如果我必须找到一个字符串名“akiko”,并且它位于表foo中,那么以下是正常的过程 select * from foo where `name = 'Akito'` 我试着检查它的两个变体 工作正常 select * from foo where name = 'Akito ' select * from foo where name = ' Akito' 工作不正常 select * from foo where name = 'Akito ' select * fro

如果我必须找到一个字符串名
“akiko”
,并且它位于表foo中,那么以下是正常的过程

select * from foo where `name = 'Akito'`
我试着检查它的两个变体

工作正常

select * from foo where name = 'Akito   '
select * from foo where name = '    Akito'
工作不正常

select * from foo where name = 'Akito   '
select * from foo where name = '    Akito'
谁能解释一下为什么第二个不起作用

提前感谢

如下所示:

特别是,尾随空格非常重要,但对于 使用=运算符执行CHAR或VARCHAR比较:


拖尾意味着不领先。这些似乎是相关的。

字符类型使用空字节填充字符串到字段的长度(而VARCHAR添加分隔符以指示字符串的结尾-从而忽略结尾的额外数据(我指的是空字节)),因此,结尾有空格的比较将忽略这些。前导空格是相关的,因为它们改变了字符串本身。见克里斯托弗的回答

编辑:需要进一步细化

请参阅下面的一些实际测试。VARCHAR类型会向字符串中添加空格,而CHAR字段,即使它们用空格填充字符串大小,在比较过程中也会忽略它们。具体参见第二行的
长度
函数查询:

mysql> create table test (a VARCHAR(10), b CHAR(10));
Query OK, 0 rows affected (0.17 sec)

mysql> insert into test values ('a', 'a'), ('a ', 'a '), (' a', ' a');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select a, LENGTH(a), b, LENGTH(b) FROM test;
+------+-----------+------+-----------+
| a    | LENGTH(a) | b    | LENGTH(b) |
+------+-----------+------+-----------+
| a    |         1 | a    |         1 | 
| a    |         2 | a    |         1 | 
|  a   |         2 |  a   |         2 | 
+------+-----------+------+-----------+
3 rows in set (0.00 sec)
其中MySQL声明CHAR字段的长度只有1个字符,插入时的值为'a'。此外,如果我们连接一些数据:

mysql> select CONCAT(a, '.'), CONCAT(b, '.') FROM test;
+----------------+----------------+
| CONCAT(a, '.') | CONCAT(b, '.') |
+----------------+----------------+
| a.             | a.             | 
| a .            | a.             | 
|  a.            |  a.            | 
+----------------+----------------+
3 rows in set (0.00 sec)

mysql> select CONCAT(a, b), CONCAT(b, a) FROM test;
+--------------+--------------+
| CONCAT(a, b) | CONCAT(b, a) |
+--------------+--------------+
| aa           | aa           | 
| a a          | aa           | 
|  a a         |  a a         | 
+--------------+--------------+
3 rows in set (0.00 sec)
您可以看到,因为VARCHAR确实存储字符串结束的位置,所以空间保留在连接上——这对于CHAR类型不适用。现在,请记住前面的
LENGTH
示例,其中第二行的字段a和b的长度不同,我们测试:

mysql> SELECT * FROM test WHERE a=b;
+------+------+
| a    | b    |
+------+------+
| a    | a    | 
| a    | a    | 
|  a   |  a   | 
+------+------+
3 rows in set (0.00 sec)
因此,我们可以总结为CHAR数据类型忽略并修剪其字符串末尾的额外空间,而VARCHAR不忽略并修剪,除非在比较期间:

那么,CHAR类型也是这样吗

mysql> select a from test where b = 'a ';
+------+
| a    |
+------+
| a    | 
| a    | 
+------+
2 rows in set (0.00 sec)

mysql> select a from test where b = 'a';
+------+
| a    |
+------+
| a    | 
| a    | 
+------+
2 rows in set (0.00 sec)

mysql> select a from test where b = ' a';
+------+
| a    |
+------+
|  a   | 
+------+
1 row in set (0.00 sec)

这表明CHAR和VARCHAR类型具有不同的存储方法,但在纯粹的字符串比较中遵循相同的规则。尾随空格被忽略;前导空格修改字符串本身。

请详细说明“在末尾忽略额外数据”是什么意思?空格不是字符串的一部分吗?让人不安的是,varchar如何保留尾随空格,但在比较过程中忽略它们。刚刚遇到此行为的问题。如果您希望(可能)使用速度较慢的方法执行此操作,请使用
select*from foo where TRIM(name)=“akio”