1. 介绍
1.1 问题描述
SELECT count(*) FROM live_play WHERE is_deleted = 0 and player like "⼩小可新"; 和 SELECT count(*) FROM live_play WHERE is_deleted = 0 and player like "小小可新"; Sql一样,但是查询出来的结果却不一样,一个显示3,一个显示0。显示3的中文是数据库直接复制出来的,显示0的是手打的汉子
1.2 问题定位
数据库 & 表 & 字段 编码都是utf8mb4 Spring Boot sql连接编码是UTF8 数据库服务端编码是utf8mb4(忘记了,可能是utf8,不是问题原因) 有的查询结果正常,个别不正常 数据库复制出来的
1.3 根本原因
中文汉字编码导致查询结果不正确,可以通过下图发现两者明显不一样。
1.4 如何产生
这个数据是db change插入进去的,查了项目,没有直接可复制的,但是看着不像是手敲的。应该是哪里复制出来的,可能是在某个编辑界面输入后复制的。
1.5 判断中文编码方式
public static void main(String[] args) {
char c = '丹';
char cc = '丹';
String ccc = "丹";
String cccc = "丹";
String ccccc = "客服系统";
System.out.printf("%c(十进制: %d,十六进制: %h)\n", c,(int)c,(int)c);
System.out.printf("%c(十进制: %d,十六进制: %h)\n", cc,(int)cc,(int)cc);
System.out.println(getEncoding(ccc));
System.out.println(getEncoding(cccc));
System.out.println(getEncoding(ccccc));
}
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s = encode;
return "GB2312";
}
} catch (Exception exception) {
exception.printStackTrace();
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s1 = encode;
return "ISO-8859-1";
}
} catch (Exception exception1) {
exception1.printStackTrace();
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s2 = encode;
return "UTF-8";
}
} catch (Exception exception2) {
exception2.printStackTrace();
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return "GBK";
}
} catch (Exception exception3) {
exception3.printStackTrace();
}
encode = "ASCII";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return "ASCII";
}
} catch (Exception exception3) {
exception3.printStackTrace();
}
encode = "GB18030";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return "GB18030";
}
} catch (Exception exception3) {
exception3.printStackTrace();
}
encode = "Unicode";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return "Unicode";
}
} catch (Exception exception3) {
exception3.printStackTrace();
}
encode = "Shift_JIS";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return "Shift_JIS";
}
} catch (Exception exception3) {
exception3.printStackTrace();
}
return "";
}
2. FAQ
3. 参考资料
【查看字符编码(简体中文)-----GBK内码查询 】