MySQL 同样也支持其他正则表达式的匹配, MySQL中使用 REGEXP 操作符来进行正则表达式匹配。
模式 | 描述 |
---|---|
^ | 匹配输入字符串的开始位置。 |
$ | 匹配输入字符串的结束位置。 |
. | 匹配除 “\n” 之外的任何单个字符。 |
[…] | 字符集合。匹配所包含的任意一个字符。例如, ‘[abc]’ 可以匹配 “plain” 中的 ‘a’。 |
[^…] | 负值字符集合。匹配未包含的任意字符。例如, ‘[^abc]’ 可以匹配 “plain” 中的’p’。 |
p1lp2lp3 | 匹配 p1 或 p2 或 p3。例如,‘zlfood’ 能匹配 “z” 或 “food”。’(zlf)ood’ 则匹配 “zood” 或 “food”。 |
* | 匹配前面的子表达式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。* 等价于{0,}。 |
+ | 匹配前面的子表达式一次或多次。例如,‘zo+’ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。+ 等价于 {1,}。 |
{n} | n 是一个非负整数。匹配确定的 n 次。例如,‘o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的两个 o |
{n,m} | m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。 |
SELECT name FROM person_tbl WHERE name REGEXP '^st';
SELECT name FROM person_tbl WHERE name REGEXP 'ok$';
SELECT name FROM person_tbl WHERE name REGEXP 'mar';
SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';
mysql中规定了很多函数,一下列出了各种函数的说明。
SELECT ASCII(CustomerName) AS NumCodeOfFirstChar
FROM Customers;
SELECT CHAR_LENGTH("RUNOOB") AS LengthOfString;
SELECT CHARACTER_LENGTH("RUNOOB") AS LengthOfString;
SELECT CONCAT("SQL ", "Runoob ", "Gooogle ", "Facebook") AS ConcatenatedString;
SELECT CONCAT_WS("-", "SQL", "Tutorial", "is", "fun!")AS ConcatenatedString;
SELECT FIELD("c", "a", "b", "c", "d", "e");
SELECT FIND_IN_SET("c", "a,b,c,d,e");
SELECT FORMAT(250500.5634, 2); -- 输出 250,500.56
SELECT INSERT("google.com", 1, 6, "runnob"); -- 输出:runoob.com
SELECT LOCATE('b', 'abc') -- 2
SELECT LCASE('RUNOOB') -- runoob
SELECT LEFT('runoob',2) -- ru
SELECT LOWER('RUNOOB') -- runoob
SELECT LPAD('abc',5,'xx') -- xxabc
SELECT LTRIM(" RUNOOB") AS LeftTrimmedString;-- RUNOOB
SELECT MID("RUNOOB", 2, 3) AS ExtractString; -- UNO
SELECT POSITION('b' in 'abc') -- 2
SELECT REPEAT('runoob',3) -- runoobrunoobrunoob
SELECT REPLACE('abc','a','x') --xbc
SELECT REVERSE('abc') -- cba
SELECT RIGHT('runoob',2) -- ob
RPAD(s1,len,s2) 在字符串 s1 的结尾处添加字符串 s2,使字符串的长度达到 len
将字符串 xx 填充到 abc 字符串的结尾处:
SELECT RPAD('abc',5,'xx') -- abcxx
SELECT RTRIM("RUNOOB ") AS RightTrimmedString; -- RUNOOB
SELECT SPACE(10);
SELECT STRCMP("runoob", "runoob"); -- 0
SELECT SUBSTR("RUNOOB", 2, 3) AS ExtractString; -- UNO
SELECT SUBSTRING("RUNOOB", 2, 3) AS ExtractString; -- UNO
SELECT SUBSTRING_INDEX('a*b','*',1) -- a
SELECT SUBSTRING_INDEX('a*b','*',-1) -- b
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d*e','*',3),'*',-1) -- c
SELECT TRIM(' RUNOOB ') AS TrimmedString;
SELECT UCASE("runoob"); -- RUNOOB
SELECT UPPER("runoob"); -- RUNOOB
SELECT ABS(-1) -- 返回1
SELECT ACOS(0.25);
SELECT ASIN(0.25);
SELECT ATAN(2.5);
SELECT ATAN2(-0.8, 2);
SELECT AVG(Price) AS AveragePrice FROM Products;
SELECT CEIL(1.5) -- 返回2
SELECT CEIL(1.5) -- 返回2
SELECT COS(2);
SELECT COT(6);
SELECT COUNT(ProductID) AS NumberOfProducts FROM Products;
SELECT DEGREES(3.1415926535898) -- 180
SELECT 10 DIV 5; -- 2
SELECT EXP(3) -- 20.085536923188
SELECT FLOOR(1.5) -- 返回1
SELECT GREATEST(3, 12, 34, 8, 25); -- 34
返回以下字符串列表中的最大值:
SELECT GREATEST("Google", "Runoob", "Apple"); -- Runoob
SELECT LEAST(3, 12, 34, 8, 25); -- 3
返回以下字符串列表中的最小值:
SELECT LEAST("Google", "Runoob", "Apple"); -- Apple
SELECT LN(2); -- 0.6931471805599453
SELECT LOG(20.085536923188) -- 3
SELECT LOG10(100) -- 2
SELECT LOG2(6); -- 2.584962500721156
SELECT MAX(Price) AS LargestPrice FROM Products;
SELECT MIN(Price) AS LargestPrice FROM Products;
SELECT MOD(5,2) -- 1
SELECT PI() --3.141593
SELECT POW(2,3) -- 8
SELECT POWER(2,3) -- 8
SELECT RADIANS(180) -- 3.1415926535898
SELECT RAND() --0.93099315644334
SELECT ROUND(1.23456) --1
SELECT SIGN(-10) -- (-1)
SELECT SIN(RADIANS(30)) -- 0.5
SELECT SQRT(25) -- 5
SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;
SELECT TAN(1.75); -- -5.52037992250933
SELECT TRUNCATE(1.23456,3) -- 1.234
SELECT ADDDATE("2017-06-15", INTERVAL 10 DAY);
->2017-06-25
SELECT ADDTIME('2011-11-11 11:11:11', 5)
->2011-11-11 11:11:16 (秒)
SELECT CURDATE();
-> 2018-09-19
SELECT CURRENT_DATE();
-> 2018-09-19
SELECT CURRENT_TIME();
-> 19:59:02
SELECT CURRENT_TIMESTAMP()
-> 2018-09-19 20:57:43
SELECT CURTIME();
-> 19:59:02
SELECT DATE("2017-06-15");
-> 2017-06-15
SELECT DATEDIFF('2001-01-01','2001-02-02')
-> -32
SELECT ADDDATE('2011-11-11 11:11:11',1)
-> 2011-11-12 11:11:11 (默认是天)
SELECT ADDDATE('2011-11-11 11:11:11', INTERVAL 5 MINUTE)
-> 2011-11-11 11:16:11 (TYPE的取值与上面那个列出来的函数类似)
SELECT DATE_FORMAT('2011-11-11 11:11:11','%Y-%m-%d %r')
-> 2011-11-11 11:11:11 AM
SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 2 DAY) AS OrderPayDate
FROM Orders
SELECT DAY("2017-06-15");
-> 15
SELECT DAYNAME('2011-11-11 11:11:11')
->Friday
SELECT DAYOFMONTH('2011-11-11 11:11:11')
->11
SELECT DAYOFWEEK('2011-11-11 11:11:11')
->6
SELECT DAYOFYEAR('2011-11-11 11:11:11')
->315
SELECT EXTRACT(MINUTE FROM '2011-11-11 11:11:11')
-> 11
SELECT FROM_DAYS(1111)
-> 0003-01-16
SELECT HOUR('1:2:3')
-> 1
SELECT LAST_DAY("2017-06-20");
-> 2017-06-30
SELECT LOCALTIME()
-> 2018-09-19 20:57:43
SELECT LOCALTIMESTAMP()
-> 2018-09-19 20:57:43
SELECT MAKEDATE(2017, 3);
-> 2017-01-03
SELECT MAKETIME(11, 35, 4);
-> 11:35:04
SELECT MICROSECOND("2017-06-20 09:34:00.000023");
-> 23
SELECT MINUTE('1:2:3')
-> 2
SELECT MONTHNAME('2011-11-11 11:11:11')
-> November
SELECT MONTH('2011-11-11 11:11:11')
->11
SELECT NOW()
-> 2018-09-19 20:57:43
SELECT PERIOD_ADD(201703, 5);
-> 201708
SELECT PERIOD_DIFF(201710, 201703);
-> 7
SELECT QUARTER('2011-11-11 11:11:11')
-> 4
SELECT SECOND('1:2:3')
-> 3
SELECT SEC_TO_TIME(4320)
-> 01:12:00
SELECT STR_TO_DATE("August 10 2017", "%M %d %Y");
-> 2017-08-10
SELECT SUBDATE('2011-11-11 11:11:11', 1)
->2011-11-10 11:11:11 (默认是天)
SELECT SUBTIME('2011-11-11 11:11:11', 5)
->2011-11-11 11:11:06 (秒)
SELECT SYSDATE()
-> 2018-09-19 20:57:43
SELECT TIME("19:30:10");
-> 19:30:10
SELECT TIME_FORMAT('11:11:11','%r')
11:11:11 AM
SELECT TIME_TO_SEC('1:12:00')
-> 4320
SELECT TIMEDIFF("13:10:11", "13:10:10");
-> 00:00:01
SELECT TIMESTAMP("2017-07-23", "13:10:11");
-> 2017-07-23 13:10:11
SELECT TO_DAYS('0001-01-01 01:01:01')
-> 366
SELECT WEEK('2011-11-11 11:11:11')
-> 45
SELECT WEEKDAY("2017-06-15");
-> 3
SELECT WEEKOFYEAR('2011-11-11 11:11:11')
-> 45
SELECT YEAR("2017-06-15");
-> 2017
SELECT YEARWEEK("2017-06-15");
-> 201724
SELECT BIN(15); -- 1111
SELECT BINARY "RUNOOB";
-> RUNOOB
SELECT CAST("2017-08-29" AS DATE);
-> 2017-08-29
SELECT COALESCE(NULL, NULL, NULL, 'runoob.com', NULL, 'google.com');
-> runoob.com
SELECT CONNECTION_ID();
-> 4292835
SELECT CONV(15, 10, 2);
-> 1111
SELECT CHARSET('ABC')
->utf-8
SELECT CHARSET(CONVERT('ABC' USING gbk))
->gbk
SELECT CURRENT_USER();
-> guest@%
SELECT DATABASE();
-> runoob
SELECT IF(1 > 0,'正确','错误')
->正确
SELECT IFNULL(null,'Hello Word')
->Hello Word
SELECT ISNULL(NULL);
->1
SELECT LAST_INSERT_ID();
->6
SELECT NULLIF(25, 25);
->
SELECT VERSION()
-> 5.6.34
眼睛:我看会了。
大脑:我都理解了。
嘴巴:真简单,这些我都会!
nia…
手: 你们会个屁。。。