MySQL里面的length函数是一个用来获取字符串长度的内置函数,一个汉字是算三个字符,中文的标点符号也是算三个字符,一个数字或字母算一个字符。具体用法示例如下:
1.查看某字符串的长度
SELECT LENGTH('www.baidu.com') FROM DUAL;
MySQL文档中对于dual表的解释:
You are allowed to specify DUAL as a dummy table name in situations where no tables are referenced:
SELECT 1+1 AS SUM FROM DUAL;
DUAL is purely for the convenience of people who require that all SELECT statements should have FROM and possibly other clauses. MySQL may ignore the clauses. MySQL does not require FROM DUAL if no tables are referenced.
2.查询文章表(article)中标题最长的10篇文章
SELECT id,title FROM article ORDER BY LENGTH(title) DESC;
3.查出用户名长度小于6个字符的用户列表
SELECT * FROM user WHERE LENGTH(username) < 6;
在MySQL内置函数里面查看字符串长度的还有一个函数是char_length,这两个函数的区别是:
length:一个汉字是算三个字符,一个数字或字母算一个字符。
char_length:不管汉字还是数字或者是字母都算是一个字符。