|
|
-- 数学运算SELECT ABS(-8) as 绝对值 -- 绝对值SELECT CEILING(9.4) -- 向上取整SELECT FLOOR(9.4) -- 向下取整SELECT RAND() -- 返回一个 0-1 之间的随机数SELECT SIGN(-10) -- 返回一个数的符号 0 返回 0 负数返回-1 正数返回 1
Related free learning recommendations: mysql video tutorial
Function |
Function |
Example |
CHAR_LENGTH(s) |
Returns the characters of string s Number |
SELECT CHAR_LENGTH('Hello 123') – 5 |
LENGTH(s) |
Return the length of string s |
SELECT LENGTH('Hello 123') – 9 |
CONCAT(s1,s2,…) |
Convert strings s1, s2 and other strings Combined into one string |
SELECT CONCAT('12','34') – 1234 |
##INSERT(s1,x,len,s2) | Replace string s2 with a string of length len starting from the x position of s1 | SELECT INSERT('12345',1,3,'abc') – abc45 |
UPPER(s),UCASE(S) | Convert all letters of string s into uppercase letters | SELECT UPPER('abc') – ABC |
LOWER(s),LCASE(s) | Convert all letters of string s into lowercase letters | SELECT LOWER('ABC') – abc |
LEFT(s,n) | Returns the first n characters of string s | SELECT LEFT('abcde',2) – ab |
RIGHT(s,n) | Returns the last n characters of string s | SELECT RIGHT('abcde',2) – de |
LPAD(s1,len,s2) | String s2 to fill the beginning of s1 so that the string length reaches len | SELECT LPAD('abc ',5,'xx') – xxabc |
##RPAD(s1,len,s2)
string s2 to fill the end of s1, so that the string The length reaches len |
SELECT RPAD('abc',5,'xx') – abcxx |
|
LTRIM(s)
Remove the string s The space at the beginning |
|
|
RTRIM(s)
Remove the space at the end of the string s |
|
|
TRIM(s)
Remove spaces at the beginning and end of string s |
|
|
TRIM(s1 FROM s)
Remove the string s1 at the beginning and end of the string s |
SELECT TRIM('@' FROM '@@abc@@ ') – abc |
|
REPEAT(s,n)
Repeat string s n times |
SELECT REPEAT('ab',3) – ababab |
|
SPACE(n)
Return n spaces |
|
|
REPLACE (s,s1,s2)
Replace string s2 with string s1 in string s |
SELECT REPLACE('abc','a','x') --xbc |
|
STRCMP(s1,s2)
Compare strings s1 and s2 |
|
| # #SUBSTRING(s,n,len)
Get the string with length len starting from the nth position in string s |
|
| MID(s,n,len)
Same as SUBSTRING(s,n,len) |
|
##LOCATE( s1,s),POSITION(s1 IN s) |
Get the starting position of s1 from the string s
SELECT LOCATE('b', 'abc') – 2 |
|
INSTR(s,s1) |
Get the starting position of s1 from string s
SELECT INSTR('abc','b') – 2 |
|
REVERSE(s) |
Reverse the order of string s
SELECT REVERSE('abc') – cba |
|
ELT(n,s1,s2,…) |
Return the nth string
SELECT ELT(2,'a','b','c' ) – b |
| ##FIELD(s,s1,s2…) | Return the first string position matching string s
SELECT FIELD ('c','a','b','c') – 3 |
| FIND_IN_SET(s1,s2) | Returns the same value as in string s2 The position of the string matched by s1
|
|
-- 字符串函数SELECT CHAR_LENGTH('我们的征途是星辰和大海') -- 字符串的长度SELECT CONCAT('我','爱','猫猫') -- 拼接字符串SELECT INSERT('我爱编程helloworld',1,2,'超级热爱') -- 查询,从某个位置开始替换某个长度SELECT LOWER('MaoMao') -- 小写字母SELECT UPPER('maomao') -- 全变大写SELECT INSTR('maonmao','n') -- 返回第一次出现的子串的索引SELECT REPLACE('猫猫说坚持就能成功','坚持','努力') -- 替换出现的指定字符串SELECT SUBSTR('猫猫说坚持就能成功',4,3) -- 返回指定的子字符串 (源字符串,截取的位置,截取的长度)SELECT REVERSE('猫猫说坚持就能成功') -- 反转-- 查询有田的同学,将田改成猪SELECT REPLACE(studentname,'田','猪') FROM studentWHERE studentname LIKE '%田'
|
Function
Function
Example |
|
##CURDATE();CURRENT_DATE() |
Return the current date
SELECT CURDATE() –> 2021-01-09
|
NOW() |
Return the current date and time |
SELECT NOW()–> 2021-01 -09 10:03:14
|
LOCALTIME() |
Return the current date and time |
SELECT LOCALTIME()–> 2021-01- 09 10:03:14
| ##UNIX_TIMESTAMP() | Return the current time in the form of UNIX timestamp | SELECT UNIX_TIMESTAMP()->1617977084
|
-- 时间和日期函数(记住)SELECT CURRENT_DATE() -- 获取当前日期SELECT CURDATE() -- 获取当前日期SELECT NOW() -- 获取当前的时间SELECT LOCALTIME() -- 本地时间SELECT YEAR(NOW())SELECT MONTH(NOW())SELECT DAY(NOW())SELECT HOUR(NOW())SELECT MINUTE(NOW())SELECT SECOND(NOW())-- 系统SELECT SYSTEM_USER()SELECT USER()SELECT VERSION()
| Aggregation function |
##Function nameDescription
COUNT() |
Count |
SUM()
Sum |
|
AVG()
Average value |
|
MAX()
Maximum value |
|
MIN()
Minimum value |
|
-- ================ 聚合函数 ============-- 都能够统计 表中的数据 (想查询一个表中有多少个记录,就是用这个count())SELECT COUNT(studentname) FROM student; -- COUNT(指定列),会忽略所有的null值SELECT COUNT(borndate) FROM student; -- 结果 8 少一个 因为是null
SELECT COUNT(*) FROM student; -- Count(*) 不会忽略所有的null值 本质 计算行数SELECT COUNT(1) FROM result; -- Count(1) 不会忽略所有的null值 本质 计算行数SELECT SUM(studentresult) AS 总和 FROM resultSELECT AVG(studentresult) AS 平均分 FROM resultSELECT MAX(studentresult) AS 最高分 FROM resultSELECT MIN(studentresult) AS 最低分 FROM result-- 查询不同课程的平均分,最高分,最低分-- 核心:根据不同的课程分组SELECT any_value(`subjectname`) AS 科目名,AVG(studentresult) AS 平均分,MAX(studentresult) AS 最高分,MIN(studentresult) AS 最低分FROM result rINNER JOIN `subject` subON r.`subjectno` = sub.`subjectno`GROUP BY r.subjectno -- 通过什么字段来分组-- 查询不同课程的平均分,最高分,最低分,平均分大于80SELECT any_value(`subjectname`) AS 科目名,AVG(studentresult) AS 平均分,MAX(studentresult) AS 最高分,MIN(studentresult) AS 最低分FROM result rINNER JOIN `subject` subON r.`subjectno` = sub.`subjectno`GROUP BY r.subjectno -- 通过什么字段来分组HAVING 平均分>50
MD5 encryption at database level |
| What is MD5
Mainly enhances algorithm complexity and irreversibility
MD5 is irreversible, the specific value of md5 is the sameMD5 The principle of cracking a website, there is a dictionary behind it , Value after MD5 encryption: Value before MD5 encryption
-- ==================== 测试MD5 加密 ===================CREATE TABLE `testmd5`(
`id` INT(4) NOT NULL,
`name` VARCHAR(20) NOT NULL,
`pwd` VARCHAR(50) NOT NULL,
PRIMARY KEY(`id`))ENGINE=INNODB DEFAULT CHARSET=utf8-- 明文密码INSERT INTO testmd5 VALUES(1,'zhangsan','123456'),(2,'lisi','123456'),(3,'wangwu','123456')-- 加密UPDATE testmd5 SET pwd=MD5(pwd) WHERE id = 1UPDATE testmd5 SET pwd=MD5(pwd) -- 加密全部的密码-- 插入的时候加密INSERT INTO testmd5 VALUES(4,'xiaoming',MD5('123456'))-- 如何校验:将用户传递进来的密码,进行md5加密,然后比对加密后的值SELECT * FROM testmd5 WHERE `name`='xiaoming' AND pwd=MD5('123456')
More related free learning recommendations:
mysql tutorial
(video)
|