MySQL:简介 Ubuntu 安装 MySQL MySQL:官方文档地址 MySQL:DDL、DQL、DML、DCL的含义 MySQL:Mac 中启动 关闭 MySQL 服务 MySQL:有哪些好用的管理工具? MySQL:命令行工具 mycli MySQL:CHAR类型 MySQL:VARCHAR类型 MySQL:整型数字 MySQL:datetime 类型 MySQL:时间戳 MySQL:创建和删除数据库 MySQL:切换和查看数据库 MySQL:创建和删除表 MySQL:在表中增加、删除、修改列 MySQL:创建和删除主键 MySQL:使用 rename 修改表名 MySQL:修改自增主键id的类型 MySQL:如何创建一个相同的表 MySQL:修改表的字符编码 MySQL:增删查改 MySQL:插入数据 MySQL:插入多行数据 MySQL:使用 insert set 插入数据 MySQL:大小写和反引号 MySQL:字符串类型值的大小写 MySQL:SQL注释 MySQL:不要使用utf8 MySQL:NULL的判等 MySQL:InnoDB存储引擎的限制 MySQL:if和case的使用 MySQL:使用 load data 快速导入数据 MySQL:使用 select into outfile 导出数据 MySQL:查询和设置 sql_mode MySQL:严格模式 MySQL:NOT NULL 字段不插入数据,会发生什么? MySQL:无符号整数列插入负数会发生什么? MySQL:关于 null 的那些事 MySQL:大表行数查询 MySQL:自动生成创建时间、更新时间;自动更新更新时间 MySQL:insert ignore MySQL:字符集排序规则 MySQL:如果连续更新一个字段两次,结果是? MySQL:字符串转数字 MySQL:尾部空格 MySQL:添加和删除索引 MySQL:唯一索引与NULL MySQL:唯一索引的单列长度限制 MySQL:InnoDB 索引 MySQL:字符集排序规则对唯一索引的影响 MySQL:唯一索引冲突消耗主键 ID MySQL 使用 index hint 指定索引:ignore index、force index、use index MySQL:查看客户端连接信息 MySQL:查看表的状态 show table status MySQL:如何治理连接数 ? MySQL:如何监控和处理慢查询与长事务 ? MySQL:自定义函数 MySQL:now() 函数 MySQL:unix_timestamp() 函数 MySQL:from_unixtime() 函数 MySQL:version() 函数 MySQL:current_timestamp() 函数 MySQL:cast 函数 MySQL:convert 函数 MySQL:使用 greatest、least 函数获取行最大值、最小值 MySQL:使用 group_concat 函数连接多行数据为一个字符串 MySQL:获取版本号 MySQL:Java 类型映射 MySQL下创建只能有一行记录的table 关于MySQL的字符集 理解数据库中的undo日志、redo日志、检查点 ubuntu下源码安装MySQL MySQL:JOIN解惑 如何快速更新数据库中的百万条数据

MySQL:尾部空格


#MySQL


结论

  • 对于char、varchar、text 等字符串类型数据,进行相等判断时,尾部空格不参与。也就是 'z''z ' 是相等的。
  • char 类型数据在存储时,会在尾部填充空格至该字段声明的长度;查询时会去掉尾部空格,具体见 MySQL:CHAR类型 。varchar、text 不会这么做。
  • 如果要精确匹配varchar、text 的尾部空格,可以用 like、长度判断等方式。

示例: 证明 char 会去掉尾部空格

建表:

use test;
CREATE TABLE `user_info` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `c1` VARCHAR(6) NOT NULL,
    `c2` CHAR(6) NOT NULL,
    `c3` TEXT,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4;

插入数据:

INSERT INTO user_info(c1, c2, c3) values
('x', 'x', 'x'), 
('x   ', 'x   ', 'x   ')
;

查询长度:

mysql root@127.0.0.1:test> select c1, length(c1), c2, length(c2), c3, length(c3) from user_info
+------+------------+----+------------+------+------------+
| c1   | length(c1) | c2 | length(c2) | c3   | length(c3) |
+------+------------+----+------------+------+------------+
| x    | 1          | x  | 1          | x    | 1          |
| x    | 4          | x  | 1          | x    | 4          |
+------+------------+----+------------+------+------------+

示例: 尾部空格的判等

use test;
CREATE TABLE `user_info` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `c1` VARCHAR(6) NOT NULL,
    `c2` CHAR(6) NOT NULL,
    `c3` TEXT,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4;

插入数据:

INSERT INTO user_info(c1, c2, c3) values
('x', 'x', 'x'), 
('x ', 'x ', 'x '), 
(' x ', ' x ', ' x ');

查询所有数据:

mysql root@127.0.0.1:test> select * from user_info;
+----+-----+----+-----+
| id | c1  | c2 | c3  |
+----+-----+----+-----+
| 1  | x   | x  | x   |
| 2  | x   | x  | x   |
| 3  |  x  |  x |  x  |
+----+-----+----+-----+

对 varchar 类型的 c1 列进行查询:

mysql root@127.0.0.1:test> select * from user_info where c1 = 'x'
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c1 = 'x '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+
2 rows in set
Time: 0.007s
mysql root@127.0.0.1:test> select * from user_info where c1 = 'x             '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c1 = ' x    '
+----+-----+----+-----+
| id | c1  | c2 | c3  |
+----+-----+----+-----+
| 3  |  x  |  x |  x  |
+----+-----+----+-----+


mysql root@127.0.0.1:test> select * from user_info where c1 = ' x'
+----+-----+----+-----+
| id | c1  | c2 | c3  |
+----+-----+----+-----+
| 3  |  x  |  x |  x  |
+----+-----+----+-----+

对 char 类型的 c2 列进行查询:

mysql root@127.0.0.1:test> select * from user_info where c2 = 'x'
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c2 = 'x '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c2 = 'x             '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c2 = ' x   '
+----+-----+----+-----+
| id | c1  | c2 | c3  |
+----+-----+----+-----+
| 3  |  x  |  x |  x  |
+----+-----+----+-----+


mysql root@127.0.0.1:test> select * from user_info where c2 = ' x'
+----+-----+----+-----+
| id | c1  | c2 | c3  |
+----+-----+----+-----+
| 3  |  x  |  x |  x  |
+----+-----+----+-----+

对 text 类型的 c3 列进行查询:

mysql root@127.0.0.1:test> select * from user_info where c3 = 'x'
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c3 = 'x '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c3 = 'x           '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
| 2  | x  | x  | x  |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c3 = ' x   '
+----+-----+----+-----+
| id | c1  | c2 | c3  |
+----+-----+----+-----+
| 3  |  x  |  x |  x  |
+----+-----+----+-----+


mysql root@127.0.0.1:test> select * from user_info where c3 = '  x   '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+


mysql root@127.0.0.1:test> select * from user_info where c3 = ' x'
+----+-----+----+-----+
| id | c1  | c2 | c3  |
+----+-----+----+-----+
| 3  |  x  |  x |  x  |
+----+-----+----+-----+

示例: 精确匹配尾部空格

建表:

use test;
CREATE TABLE `user_info` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `c1` VARCHAR(6) NOT NULL,
    `c2` CHAR(6) NOT NULL,
    `c3` TEXT,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4;

插入数据:

INSERT INTO user_info(c1, c2, c3) values
('x', 'x', 'x'), 
('x   ', 'x   ', 'x   ')
;

对于 varchar:

mysql root@127.0.0.1:test> select * from user_info where c1 like 'x'
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+
| 1  | x  | x  | x  |
+----+----+----+----+

mysql root@127.0.0.1:test> select * from user_info where c1 like 'x '
+----+----+----+----+
| id | c1 | c2 | c3 |
+----+----+----+----+

mysql root@127.0.0.1:test> select * from user_info where c1 like 'x   '
+----+------+----+------+
| id | c1   | c2 | c3   |
+----+------+----+------+
| 2  | x    | x  | x    |
+----+------+----+------+


mysql root@127.0.0.1:test> select * from user_info where c1 = 'x   '
+----+------+----+------+
| id | c1   | c2 | c3   |
+----+------+----+------+
| 1  | x    | x  | x    |
| 2  | x    | x  | x    |
+----+------+----+------+

mysql root@127.0.0.1:test> select * from user_info where c1 = 'x   ' and length(c1) = 4
+----+------+----+------+
| id | c1   | c2 | c3   |
+----+------+----+------+
| 2  | x    | x  | x    |
+----+------+----+------+



( 本文完 )