Illegal mix of collations for operation 'concat'

在t_employee表中,练习使用concat函数连接字符串时,

mysql> select concat('工号为:',fnumber,'的员工的幸福指数:',fsalary/(fage-21))
-> from t_employee;
1271 - Illegal mix of collations for operation 'concat'   报错

在网上查看了相关错误文章发现是字段得字符集有问题,我这里的fnumber字段的字符集是latin1,而字段操作默认为UTF8的编码。 绝对统一使用UTF-8 。

修改了字符集为utf-8后再次运行SQL命令

mysql> select concat('工号为:',fnumber,'的员工的幸福指数:',fsalary/(fage-21))
-> from t_employee;
+-----------------------------------------------------------------+
| concat('工号为:',fnumber,'的员工的幸福指数:',fsalary/(fage-21)) |
+-----------------------------------------------------------------+
| 工号为:dev001的员工的幸福指数:2075.000000 |
| 工号为:dev002的员工的幸福指数:328.685714 |
| 工号为:HR001的员工的幸福指数:1100.400000 |
| 工号为:HR002的员工的幸福指数:1300.090000 |
| 工号为:IT001的员工的幸福指数:557.142857 |
| 工号为:IT002的员工的幸福指数:466.666667 |
| 工号为:sales001的员工的幸福指数:2500.000000 |
| 工号为:sales002的员工的幸福指数:885.714286 |
| 工号为:sales003的员工的幸福指数:1200.000000 |
+-----------------------------------------------------------------+
9 rows in set

搜索出来的数据是正确的,没有报错。

原文地址:https://www.cnblogs.com/lisuyun/p/4191490.html