MySQL View - 非法混合排序规则

标签 mysql sql mysql-error-1064 collation

我会很清楚:在 MySQL 中创建 View 而不会出现该死的Illegal mix of collat​​ions 错误的解决方案是什么。

我的 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 collat​​ions ( 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/

相关文章:

mysql - 如何更新mysql数据库中的unix时间戳

sql - 在 SQL 中打破参数并进行搜索

sql - 有没有办法在不读取 sql 列的情况下增加它的值

php - MySQL 语法 :You have an error in your SQL syntax

指定 LIMIT 时 MySQL 错误 1064

php - MySQL/PHP : nonrepeating random selection with unique ID selectors

mysql - 如何在表中为所有具有相同名称的记录设置相同的列ID号

mysql - 在mysql中选择总和值的最大值

sql - 在不允许嵌套 SELECTS 时选择不在 GROUP BY 中的字段

sql - 映射两个没有关系且没有循环的表