Mysql設置字符編碼可以解決一些常見的問題,比如使用不同的字符集出錯的問題,下面就是Mysql設置字符編碼解決該問題的具體介紹。
錯誤是在你的結果集中有兩種字符集。
比如說你在兩個表聯合查詢,一個表的字符集是latin1,另一個是utf8,
這樣在你的結果集中有兩種字符集,mysql會報錯誤。
一個表中不同的字段使用不同的字符集,也是一個道理。
用SHOW CREATE TABLE table_name;可以看出具體的字符集設置。
查了幫助手冊,說是user的字符集沒有設,默認為utf8,將其轉為latin1或gb2312等字符集
解決方法:
將不同的字符集,轉化成統一的字符集。 下面就是Mysql設置字符編碼的方法。
- After an upgrade to MySQL 4.1, the statement fails:
- mysql> SELECT SUBSTRING_INDEX(USER(),'@',1);
- ERROR 1267 (HY000): Illegal mix of collations
- (utf8_general_ci,IMPLICIT) and (latin1_swedish_ci,COERCIBLE)
- for operation 'substr_index'
- The reason this occurs is that usernames are stored using UTF8 (see section 11.6 UTF8 for Metadata). As a result, the USER() function and the literal string '@' have different character sets (and thus different collations):
- mysql> SELECT COLLATION(USER()), COLLATION('@');
- +-------------------+-------------------+
- | COLLATION(USER()) | COLLATION('@') |
- +-------------------+-------------------+
- | utf8_general_ci | latin1_swedish_ci |
- +-------------------+-------------------+
- One way to deal with this is to tell MySQL to interpret the literal string as utf8:
- mysql> SELECT SUBSTRING_INDEX(USER(),_utf8'@',1);
- +------------------------------------+
- | SUBSTRING_INDEX(USER(),_utf8'@',1) |
- +------------------------------------+
- | root |
- +------------------------------------+
- Another way is to change the connection character set and collation to utf8. You can do that with SET NAMES 'utf8' or by setting the character_set_connection and collation_connection system variables directly.
表的編碼轉換可以用:MySQL Version > 4.12)
- ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
之前的版本可以用:
- ALTER TABLE tbl_name CHARACTER SET charset_name;
mysql修改字段的語句寫法
MySQL中多表刪除方法
詳解MySQL如何鏈接遠程SQL
MySQL集群簡介與配置詳解
MySQL數據庫的23個特別注意事項