MySQL两种识别是否有中文字符的方法
来源:站长家园 时间:2011-01-04 20:11:00
1) 用正式表达式 regexp "[u0391-uFFE5]"
2) 用length和char_length
drop table t1;
create table t1 ( id varchar(200)) default charset=utf8 ;
insert into t1 values ('中文'),('sdalfkj');
1)用正式表达式 regexp "[u0391-uFFE5]"
root@127.0.0.1 : test 21:55:33> select id ,id regexp "[u0391-uFFE5][u0391-uFFE5]" from t1;
+---------+--------------------------------------------+
| id | id regexp "[u0391-uFFE5][u0391-uFFE5]" |
+---------+--------------------------------------------+
| 中文 | 0 |
| sdalfkj | 1 |
+---------+--------------------------------------------+
2 rows in set (0.01 sec)
2) 用length和char_length
root@127.0.0.1 : test 23:33:13> select id,length(id),char_length(id) from t1;
+---------+------------+-----------------+
| id | length(id) | char_length(id) |
+---------+------------+-----------------+
| 中文 | 6 | 2 |
| sdalfkj | 7 | 7 |
+---------+------------+-----------------+
2 rows in set (0.00 sec)