存储过程报Illegal mix of collations错误的解决方法

CREATE PROCEDURE maxAgeStudent(IN _gender CHAR)
BEGIN
	DECLARE maxage INT DEFAULT 0;
	
	SELECT max(age) INTO maxage FROM student where gender = _gender;
	SELECT * from student WHERE age = maxage and gender = _gender;
END;

在调用的时候

call maxAgeStudent('1')

产生了报错信息:

1267 - Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation '='

产生错误的原因就是=两端进行比较的gender和_gender所采用的字符集排序方式不同。

最简单的解决办法就是给IN参数显示的设定一个字符集:

CREATE PROCEDURE maxAgeStudent(IN _gender CHAR CHARSET utf8mb4)
BEGIN
	DECLARE maxage INT DEFAULT 0;
	
	SELECT max(age) INTO maxage FROM student where gender = _gender;
	SELECT * from student WHERE age = maxage and gender = _gender;
END;

这样问题就解决了

你可能感兴趣的:(数据库,存储过程)