通过编码查看字符串字符集暂用空间大小
- SET @s = CONVERT('abc' USING utf8);
SELECT LENGTH(@s),CHAR_LENGTH(@s); - SET @s = CONVERT('abc' USING ucs2);
SELECT LENGTH(@s),CHAR_LENGTH(@s);
使用show collation来查看所有排序规则
- SHOW COLLATION LIKE 'latin1%';
- SHOW CHARACTER SET ; (字符串会使用字符集中Defaults列为YES排序规则,latin1默认规则是latin1_swedish_ci,你也可以使用SHOW CHARACTER SET 查看默认排序规则)
- CREATE TABLE t1(c CHAR(3) CHARACTER SET latin1); (创建一个以latin1字符串列)
INSERT INTO t1(c) VALUES('AAA'),('bbb'),('aaa'),('BBB');
SELECT c FROM t1; - SELECT c FROM t1 ORDER BY c COLLATE latin1_swedish_ci; (不区分大小写排序规则)
- SELECT c FROM t1 ORDER BY c COLLATE latin1_general_cs;(区分大小写排序规则)
- SELECT c FROM t1 ORDER BY c COLLATE latin1_bin; (使用数字值对字符进行排序)
- CREATE TABLE t2(c CHAR(2) CHARACTER SET utf8);
INSERT INTO t2 (c) VALUES('cg'),('ch'),('ci'),('lk'),('ll'),('lm');
SELECT c FROM t2 ORDER BY c COLLATE utf8_general_ci; (使用utf8_general_ci排序)
SELECT c FROM t2 ORDER BY c COLLATE utf8_spanish2_ci; (使用utf8_spanish2_ci排序) - CREATE TABLE t (c1 CHAR(10), c2 VARCHAR(10)); (使用CHAR和VARCHAR两种存储值长度)
INSERT INTO t (c1,c2) VALUES ('abc ','abc ');
SELECT c1,c2, CHAR_LENGTH(C1),CHAR_LENGTH(C2) FROM t;
设置客户端连接字符集
- SET NAMES 'utf8';
- SET NAMES 'utf8' COLLATE 'utf8_general_ci';
字符串字面量定义
- SELECT "I'm asleep",'He said,"Boo"'; (使用双引号将单引号字符串括起来(假设禁用了ANSI_QUOTES))
- SELECT "He said, ""Boo!""","And I said,\""Yikes!\"";
- SELECT 'I''m asleep','I\'m wide awake'; (字符串内使用相同引号字符,那么需要双写引号或前面加上反斜杠)
- SELECT "He said, ""Boo!""","And I said,\"Yikes!\"";
- SELECT 'Install MySql in c:\mysql on windows'; (反斜杠会取消后续字符特殊含义,包括另一个反斜杠字符)
检查或更改字符串字符集或排序规则
- SELECT USER(),CHARSET(USER()),COLLATION(USER()); (使用CHARSET()或COLLATION()函数排序)
- SET NAMES latin1;
SELECT CHARSET('abc'),COLLATION('abc'); - SET NAMES UTF8 COLLATE 'utf8_bin';
SELECT CHARSET('abc'),COLLATION('abc'); - SET @s1 = _latin1 'my string',@s2 = CONVERT(@s1 USING utf8);
SELECT CHARSET(@s1),CHARSET(@s2); - SET @s1 =_latin1 'my string',@s2 = @s1 COLLATE latin1_spanish_ci;
SELECT COLLATION(@s1),COLLATION(@s2); - SET @s1 = _latin1 'my string';
SET @s2 = CONVERT(@s2 USING utf8) COLLATE utf8_spanish_ci;
SELECT CHARSET(@s1),COLLATION(@s1),CHARSET(@s2),COLLATION(@s2); - SET @s1 = _latin1 'my string';
SET @S2 = CONVERT(@s1 USING binary); (使用CONVERT()函数将二进制转换为非二进制)
SET @s3 = CONVERT(@s2 USING utf8);
SELECT CHARSET(@s1),CHARSET(@s2),CHARSET(@s3); - SELECT CHARSET(BINARY _latin1 'my string'); (使用binary运算符生成二进制)
转换字符串大小写
- SELECT thing,UPPER(thing),LOWER(thing) FROM limbs;
- SELECT b,UPPER(CONVERT(b USING latin1)) AS upper,LOWER(CONVERT(b USING latin1)) AS lower FROM t; (将二进制字符串大小写,转换成非二进制字符串.)
- CREATE FUNCTION initial_cap(s VARCHAR(255)) RETURNS VARCHAR(255) DETERMINISTIC RETURN CONCAT(UPPER(LEFT(s,1)),MID(s,2)); (生成一个函数,将字符串大小写,分成几部分,然后进行重新组合,把字符串转换成大写.)
- SELECT thing,initial_cap(thing) FROM limbs; (通过上面函数将首字母转换成大写)
- SET @s1 = BINARY 'cat',@s2 = BINARY 'CAT';
SELECT @s1 = @s2; (不相等二进制字符串进行比较) - SET @S1 = CONVERT(@s1 USING latin1) COLLATE latin1_swedish_ci;
SET @S2 = CONVERT(@s2 USING latin1) COLLATE latin1_swedish_ci;
SELECT @s1 = @s2; (使用latin_swedish_ci进行排序比较)
利用SQL模式进行模式匹配
- SELECT name FROM metal WHERE name LIKE 'me%'; (使用SQL模式进行匹配)
- SELECT name FROM metal WHERE name LIKE '%d';
- SELECT name FROM metal WHERE name LIKE '%in%';
- SELECT name FROM metal WHERE name NOT LIKE '%i%';
- SELECT NULL LIKE '%',NULL NOT LIKE '%';
使用正则表达式进行模式匹配
模式字符 定义
^ 匹配字符串开始
$ 匹配字符串结尾
. 匹配任意单个字符
[...] 匹配括号内的任意一个字符
[^...] 匹配除了括号内所列字符之外任意一个字符
p1|p2|p3 选择性匹配,匹配p1,p2,p3中任意一个模式
- 匹配前面字表达式零次或多次
- 匹配前面字表达式一次或多次
{n} 匹配前面子表达式N次
{m,n} 匹配前面子表达式最少M次,最多N次
- SELECT name FROM metal WHERE name REGEXP '^me'; (以me开头)
- SELECT name FROM metal WHERE name REGEXP 'd$'; (以d结尾)
- SELECT name FROM metal WHERE name REGEXP 'in'; (任意特定字符串)
- SELECT name FROM metal WHERE name REGEXP '^..at'; (特定位置包含字符串)
POSIX class 匹配定义
[:alnum:] 字母和数字
[:alpha:] 字母
[:blank:] 空格和制表符
[:cntrl:] 控制字符
[:digit:] 数字
[:graph:] 可见字符(不包括空格)
[:lower:] 小写字母
[:print:] 可见字符(包括空格)
[:punct:] 标点符号
[:space:] 空格,制表符,换行,回车
[:upper:] 大写字母
[:xdigit:] 十六进制(0-9,a-f,-F)
- SELECT name,name REGEXP '[[:xdigit:]]' FROM metal; (匹配以aeiou开头,以d结尾)
分隔或连接字符串
- SET @date = '2020-04-06';
SELECT @date, LEFT(@date,4) AS year, MID(@date,6,2) AS month, RIGHT(@date,2) AS day; - SET @date = '2020-04-06';
SELECT @date,SUBSTRING(@date,6),MID(@date,6); - SET @email = 'postmaster@ekinshen.com';
SELECT @email,SUBSTRING_INDEX(@email,'@',1) AS user,SUBSTRING_INDEX(@email,'@',-1) AS host; (SUBSTRING_INDEX(str,c,n))将返回指定字符串右侧或左侧所有内容 - SELECT name from metal WHERE LEFT(name,1) >= 'n'; (第一个字母>=n)
- SELECT CONCAT(name,'ends in "d":',IF(RIGHT(name,1)='d','YES','NO')) AS 'ends in "d"?' FROM metal; (使用CONCAT函数进行连接)
搜索子字符串
- SELECT name,LOCATE('in',name),LOCATE('in',name,3) FROM metal;
- SELECT name ,name LIKE '%in%',name REGEXP 'in' FROM metal;
来源:oschina
链接:https://my.oschina.net/u/4321806/blog/3275782