Mysql case 视图操作报错 1267 Illegal mix of collations (utf8mb4_unicode_ci,COERCIBLE)……

报错

视图(view)查询时报错:General error: 1267 Illegal mix of collations (utf8mb4_unicode_ci,COERCIBLE) and (utf8mb4_general_ci,COERCIBLE) for operation 'case' 

报错语句

SELECT
	`test`.`score_item_id` AS `score_item_id`,
	`test`.`title` AS `title`,
	`test`.`check_year` AS `check_year`,
	`test`.`check_month` AS `check_month`,
	sum((
		CASE
				`test`.`check_month` 
				WHEN '01' THEN
				1 ELSE 0 
			END 
			)) AS `january` 
	FROM
		`test` 
	GROUP BY
		`test`.`type_id`,
		`test`.`check_year` 
	ORDER BY
	`test`.`check_year` DESC,
	`test`.`score_item_id`

处理

从报错中可以看出是case操作时的字段出现了字符集混乱问题,由于我数据库默认设置的字符集是utf8mb4_general_ci,所以我直接把字符集转为utf8mb4,处理后的语句如下,在case字段出加了convert()函数对字段字符集进行转换

SELECT
	`test`.`score_item_id` AS `score_item_id`,
	`test`.`title` AS `title`,
	`test`.`check_year` AS `check_year`,
	`test`.`check_month` AS `check_month`,
	sum((
		CASE
				CONVERT ( `test`.`check_month` USING utf8mb4 ) 
				WHEN '01' THEN
				1 ELSE 0 
			END 
			)) AS `january` 
	FROM
		`test` 
	GROUP BY
		`test`.`type_id`,
		`test`.`check_year` 
	ORDER BY
	`test`.`check_year` DESC,
	`test`.`score_item_id`

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值