两张表的字段分别是两种不同的比较规则,在进行联表查询的时候就会出现上述错误。
字符比较规则有各个层级的,比如数据库、表、字段,这里只需要保证字段的比较规则一致即可。但是为了整体一致,除了特殊的业务需求,我们最好还是保证库、表、字段的比较规则一致。

-- ----------------------------
-- Table structure for administrative_area
-- ----------------------------
DROP TABLE IF EXISTS `administrative_area`;
CREATE TABLE `administrative_area` (
`id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`create_time` datetime(6) DEFAULT NULL,
`create_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_update_time` datetime(6) DEFAULT NULL,
`last_update_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`level` int(0) NOT NULL,
`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`parent_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;

这里给出一张表结构,然后在这个表结构的基础上,把表和字段都调整为utf8mb4_bin,给出修正的例子:

------表修改------

show table status from dubbo_play like '%administrative_area%';

此时表的collations如下图所示:

此时修改表的collate
ALTER TABLE `administrative_area` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin;
此时的结果:

------字段修改------
修改完表的比较规则之后,我们看看字段的状态:

所以如果此时执行联表查询,还是会报错,我们需要把字段修改为相同的比较规则:

ALTER TABLE `administrative_area` MODIFY `id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL;
ALTER TABLE `administrative_area` MODIFY `create_time` datetime(6) DEFAULT NULL;
ALTER TABLE `administrative_area` MODIFY `create_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL;
ALTER TABLE `administrative_area` MODIFY `last_update_time` datetime(6) DEFAULT NULL;
ALTER TABLE `administrative_area` MODIFY `last_update_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL;
ALTER TABLE `administrative_area` MODIFY `code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL;
ALTER TABLE `administrative_area` MODIFY `level` int(0) NOT NULL;
ALTER TABLE `administrative_area` MODIFY `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL;
ALTER TABLE `administrative_area` MODIFY `parent_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL;

此时的字段状态:

posted on 2022-01-12 21:48  长江同学  阅读(15328)  评论(0编辑  收藏  举报