【问题标题】:MySQL castr operatorMySQL castr 运算符
【发布时间】:2013-09-28 07:00:36
【问题描述】:

以下查询是否有任何更短的等效项:(我无法更改表)。强制转换运算符在这里适用吗?

select convert(old_text using utf8) as text,
   convert(rev_timestamp using utf8) as ts,
   convert(rev_user_text using utf8) as user from revision;

更新:表架构是:

CREATE TABLE `revision` (
    `rev_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `rev_page` INT(10) UNSIGNED NOT NULL,
    `rev_text_id` INT(10) UNSIGNED NOT NULL,
    `rev_comment` TINYBLOB NOT NULL,
    `rev_user` INT(10) UNSIGNED NOT NULL DEFAULT '0',
    `rev_user_text` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'latin1_bin',
    `rev_timestamp` BINARY(14) NOT NULL DEFAULT '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0',
`old_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`old_text` MEDIUMBLOB NOT NULL,
`old_flags` TINYBLOB NOT NULL,
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB

【问题讨论】:

    标签: mysql casting


    【解决方案1】:

    最简单的修改方法,就是修改mysql字符集key中的my.ini文件,

    default-character-set = utf8 
    character_set_server = utf8
    

    修改后重启mysql服务,服务mysql重启

    【讨论】:

    • 错了。这不会转换任何东西。新插入的数据将是 ut8 是的,但没有别的。
    【解决方案2】:
    set names utf8;
    
    SELECT old_text AS text, rev_timestamp AS ts, rev_user_text AS user
    FROM revisions;
    

    ...但是如果您以不同的字符集发送 DML,这将中断。

    【讨论】:

    • "does not work" 不是对发生的有意义的描述,也不是错误消息。
    【解决方案3】:

    最好的办法是改变你的表格并为你需要的列设置 utf8,而不是每次都转换它们

        ALTER TABLE t MODIFY old_text CHAR(50) CHARACTER SET utf8;
        ALTER TABLE t MODIFY rev_timestamp CHAR(50) CHARACTER SET utf8;
        ALTER TABLE t MODIFY rev_user_text CHAR(50) CHARACTER SET utf8;
    

    然后正常选择它们。

    select old_text  as text, rev_timestamp  as ts, rev_user_text as user 
    from revision;
    

    注意:关于 char(50) 将其更改为您的需要。

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多