我会很清楚:在 MySQL 中创建 View 而不会出现该死的Illegal mix of collations 错误的解决方案是什么。
我的 SQL 代码是这样的(它有一些葡萄牙语单词),我的数据库默认排序规则是 latin1_swedish_ci:
CREATE VIEW v_veiculos AS
SELECT
v.id,
v.marca_id,
v.modelo,
v.placa,
v.cor,
CASE v.combustivel
WHEN 'A' THEN 'Álcool'
WHEN 'O' THEN 'Óleo Diesel'
WHEN 'G' THEN 'Gasolina'
ELSE 'Não Informado'
END AS combustivel,
marcas.marca,
/*I think that the CONCAT and COALESCE below causes this error, when the next line the view works fine*/
CONCAT(marca, ' ', v.modelo, ' - Placa: ', v.placa, ' - Combustível: ', COALESCE(v.combustivel, 'Não informado')) AS info_completa
FROM veiculos v
LEFT JOIN
marcas on(marcas.id = v.marca_id);
我认为错误原因是因为我正在使用 coalesce 和/或 concat 作为完整错误的描述告诉我:Illegal mix of collations ( latin1_swedish_ci,IMPLICIT) 和 (utf8_general_ci,COERCIBLE) 用于“合并”操作
最佳答案
您也可以使用 CAST()
将字符串转换为不同的字符集。语法是:
CAST(character_string AS character_data_type CHARACTER SET charset_name)
例如:
SELECT CAST(_latin1'test' AS CHAR CHARACTER SET utf8);
备选方案:使用 CONVERT(expr USING transcoding_name)
关于MySQL View - 非法混合排序规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14069506/