错误是在你的结果集中有两种字符集。
比如说你在两个表联合查询,一个表的字符集是latin1,另一个是utf8,
这样在你的结果集中有两种字符集,mysql
会报上面的错误。
一个表中不同的字段使用不同的字符集,也是一个道理。
用SHOW CREATE TABLE table_name;可以看出具体的字符集设置。
查了帮助手册,说是user的字符集没有设,默认为utf8,将其转为latin1或gb2312等字符集
解决方法:
将不同的字符集,转化成统一的字符集。
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
官方解释:
http://dev.mysql
.com/doc/refman/5.0/en/charset
-collation-charset
.html
附加:
修改字段类型的方法:(经过 google 搜索)
登录MYSQL
后执行这个语句就可以了:
复制SQL代码保存代码ALTER TABLE `phrase` CHANGE `varname` `varname` VARCHAR( 250 ) NOT NULL;
修改字段字符集的方法:
mysql
〉show full columns from table就会发现两个字段的字符集是不一样的。
通过用
ALTER TABLE `tname` CHANGE `column` `column` VARCHAR( 15 ) CHARACTER SET latin1 NOT NULL。
(假设变更为字符型,並且字符集类别为latin1 latin1 )
修改.ini文件的方法:(不建议使用)
在mysql
的安装目录下有一个my.ini文件,
打开找到[mysql
]下的default-character-set=*****,改成default-character-set=gb2312,重启mysql
即可。
From:
http://darrendu.javaeye.com/blog/255538