1267 - Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for opera

數據庫報錯之 1267 - Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=’

數據庫報錯之 1267 - Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=’web

原 SQL :

SELECT
	d.shopname,
	c.goodsno,
	s.id
FROM
	t_oversea_demand d
	LEFT JOIN t_overseapurchasing c ON d.id = c.demandID
	LEFT JOIN d_shop s ON  s.`name` = d.shopname
WHERE
	d.STATUS = '5' 
	AND d.updatetime > '2020-02-09' 
GROUP BY
	CONCAT( d.shopname, c.goodsno )

報錯:

SELECT
	d.shopname,
	c.goodsno,
	s.id
FROM
	t_oversea_demand d
	LEFT JOIN t_overseapurchasing c ON d.id = c.demandID
	LEFT JOIN d_shop s ON  s.`name` = d.shopname
WHERE
	d.STATUS = '5' 
	AND d.updatetime > '2020-02-09' 
GROUP BY
	CONCAT( d.shopname, c.goodsno )
> 1267 - Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='
> 時間: 0.001s

分析:

關聯表 d_shop 與 主表 t_oversea_demand  表結構的編碼不一樣 因此不能用來作等式

解決:

方案1 修改表結構
ALTER TABLE `表名` CONVERT TO CHARACTER SET utf8 collate utf8_unicode_ci
方案2 使用 CONVERT 。以前這個表使用的編碼不一樣是有緣由的 不能隨便更改 因而乎…
SELECT
   d.shopname,
   c.goodsno,
   s.id
FROM
   t_oversea_demand d
   LEFT JOIN t_overseapurchasing c ON d.id = c.demandID
   LEFT JOIN d_shop s ON CONVERT ( s.`name` USING utf8 ) = CONVERT ( d.shopname USING utf8 )
WHERE
   d.STATUS = '5' 
   AND d.updatetime > '2020-02-09' 
GROUP BY
   CONCAT( d.shopname, c.goodsno )

就是把編碼不一樣到兩個表字段臨時轉碼處理如下就行了數據庫

完美解決