如何使用SQL在MySQL数据库中查找中文短语的长度?
For example, this is my table, which is called example:
--------------------------
| id | en_word | zh_word |
--------------------------
| 1 | Internet| 互联网 |
--------------------------
| 2 | Hello | 你好 |
--------------------------
and so on...
And I tried using this SQL Query:
SELECT * FROM `example` WHERE LENGTH(`zh_word`) = 3
For some reason, it wouldn't give me three, but would give me a lot of single letter characters.
Why is this? Can this be fixed? I tried this out in PhpMyAdmin.
But when I did it with JavaScript:
"互联网".length == 3; // true
And it seems to work fine. So how come it doesn't work?
例如,这是我的表,名为example: p>
-------------------------- \ N | id | en_word | zh_word | -------------------------- | 1 | 互联网| 互联网| -------------------------- | 2 | 你好| 你好| -------------------------- 等等...... code> pre>我尝试使用这个SQL查询: p>
SELECT * FROM`example` WHERE LENGTH(`zh_word`)= 3 code> pre>
出于某种原因,它不会给我三个,但会给我很多单字母字符。 p>
为什么会这样? 这可以修复吗? 我在PhpMyAdmin中试过这个。 p>
但是当我用JavaScript做的时候: p>
“互联网”.length == 3; // true code> pre>
它似乎工作正常。 那怎么会不起作用? p> div>
you should use CHAR_LENGTH
instead of LENGTH
LENGTH()
returns the length of the string measured in bytes. CHAR_LENGTH()
returns the length of the string measured in characters.
LENGTH returns length in bytes (and chinese is multibyte)
Use CHAR_LENGTH to get length in characters
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_length