Mysql字符的使用
SELECT ASCII(user_password) as 阿斯克码 FROM users;#返回首字符的ascii码 SELECT CHAR_LENGTH(user_password) as 字符长度 FROM users;#返回字符长度 SELECT CHARACTER_LENGTH(user_password) as 字符长度 FROM users;#返回字符长度 SELECT CONCAT(id,user_password,class_id) as 拼接字符 from users;#返回拼接字符串 SELECT CONCAT_WS('_',id,user_password,class_id) as 拼接字符 from users;#返回带符号的拼接字符串 SELECT FIELD("杜甫",user_password) 查找的列 from users;#返回对应的位置 SELECT FIND_IN_SET("武则天",user_password) 查找对应字符 from users;#返回字符存在的位置
SELECT FORMAT(2000.54556,2) as 格式 ;#返回保留2位小数的字符 SELECT INSERT("baidu.com",1,5,"bing") as 改变链接;#返回第一位开始到第五位替换成新字符串bing的字符 SELECT LOCATE("武",user_password) as 位置 from users;#返回字符所在的位置,和find_in_set不同的是不需要完全匹配 SELECT POSITION("武" IN user_password) as 位置 from users;#返回字符所在的位置,和find_in_set不同的是不需要完全匹配,与locate相同 SELECT LCASE("Hallo World") AS 转换为小写;#返回转换为小写的字符 SELECT LEFT(user_password,1) As 首字符 from users;#返回1或n个长度的字符 SELECT LOWER("3DM") as 转换为小写;#转换为小写字符 SELECT LPAD("new",5,"real") as 拼接;#将real拼接到new前面直到变成5个字符,太长就删除多余字符 SELECT LTRIM(" 李白") as 去除空格后;#去除左边的空白字符串 SELECT MID("两只老虎爱跳舞",3,5) as 截取;#截取第3个字符开始的五个字符同substring
SELECT REPEAT(user_password,3)as 自我拼接 from users;#字符串自我拼接3次 select REPLACE('好好学习,天天向上','学习','奋斗') as 替换;#把好好学习,天天向上的学习替换为奋斗 select REVERSE(user_password) as 翻转 from users;#反转字符串 SELECT RIGHT(user_password,2) as 后面两个字符 from users;#返回后面两个字符,和left相反 SELECT RPAD('happy',10,'new year') as 拼接字符;#把new year拼接到happy右边直到变成是个字符,多余的删除 SELECT RTRIM("李红 ") as 去除后面的空字符;#去除后面的空字符 SELECT CONCAT_WS(SPACE(10),id,user_password,class_id) as 空字符拼接 from users;#space返回10个空字符 SELECT STRCMP('李世民','李二') as 返回值;#返回表达式大小,如:李世民<李二,所以返回-1;若李世民=李二,返回0;李世民大于李二,返回1 SELECT SUBSTR('happy',2,3) as 截取字段;#截取第二个字符开始3个字符和mid一样 SELECT substring('happy',2,3) as 截取字段;#截取第二个字符开始3个字符
SELECT UCASE("abcd") as 转换为大写;#返回大写 SELECT UPPER("chinese") as 转换为大写;#返回大写 SELECT TRIM(" 李白 ") as 去掉前后的空格;#去除前后空格 SELECT SUBSTRING_INDEX("今晚打,老虎",",",1) as 截取;#以逗号为分割点,1返回左边的今晚打,-1返回老虎