当前位置:网站首页>MySQL-05

MySQL-05

2022-06-26 05:58:00 Mr.Rop

5、MySQL function

5.1 Common functions

-- ================  Common functions  ===============
--  Mathematical operations 
SELECT ABS(-8)  --  The absolute value 
SELECT CEILING(9.4)  --  Rounding up 
SELECT FLOOR(9.4)  --  Want to round down 
SELECT RAND()  --  Return to one 0-1 Random number between 
SELECT SIGN(10)  --  Judge the sign of a number  0-0, A negative number returns -1, A positive number returns 1

--  String function 
SELECT CHAR_LENGTH(' Even the smallest sail can sail far ')  --  String length 
SELECT CONCAT(' I ',' It's called ',' What ',' Well ')  --  String splicing 
SELECT INSERT(' I love programming. helloworld',1,2,' Super love ')  --  Inquire about , Replace ( Replace a certain length from a certain position )
SELECT UPPER('KuangShen')  --  Convert all to uppercase 
SELECT LOWER('KuangShen')  --  Convert all to lowercase 
SELECT INSTR('kuangshen','h')  --  Returns the index of the first occurrence of the substring 
SELECT REPLACE(' Crazy God says persistence can succeed ',' insist ',' Strive ')  --  Replace the occurrence of the specified string 
SELECT SUBSTR(' Crazy God says persistence can succeed ',4,6)  --  Returns the specified string ( Original string , Intercept location , Interception length )
SELECT REVERSE(' It was a pig ')  --  reverse 

--  Inquire about the classmate surnamed Zhang , name -> Zou 
SELECT REPLACE(StudentName,' Zhang ',' Zou ') FROM student
WHERE StudentName LIKE ' Zhang %'

--  Event and date functions ( remember )
SELECT CURRENT_DATE() --  Get current date 
SELECT CURDATE()  --  Get current date 
SELECT NOW()  --  Get current date , Time 
SELECT LOCALTIME()  --  Local time 
SELECT SYSDATE()  --  system time 

SELECT YEAR(NOW())
SELECT MONTH(NOW())
SELECT DAY(NOW())
SELECT HOUR(NOW())
SELECT MINUTE(NOW())
SELECT SECOND(NOW())

--  System 
SELECT SYSTEM_USER()
SELECT USER()
SELECT VERSION()

5.2 Aggregate functions ( Commonly used )

The name of the function describe
COUNT() Count
SUM() Sum up
AVG() Average
MAX() Maximum
MIN() minimum value
-- ==========  Aggregate functions  ==============
--  Can count   Table data ( Want to query how many records there are in a table , Just use count())
SELECT COUNT(StudentName) FROM student; -- count( Field ), Will ignore all null value 
SELECT COUNT(*) FROM student -- count(*), Will not ignore null value , The essence   Count lines 
SELECT COUNT(1) FROM student -- count(1), Will not ignore all null value   The essence   Count lines 

SELECT SUM(`StudentResult`) AS  The sum of the  FROM result;
SELECT AVG(`StudentResult`) AS  average  FROM result;
SELECT MAX(`StudentResult`) AS  The highest  FROM result;
SELECT MIN(`StudentResult`) AS  Lowest score  FROM result;

5.3、 database-level MD5 encryption ( Expand )

What is? MD5?

It mainly enhances the complexity and irreversibility of the algorithm

MD5 Irreversible , Specific values MD5 It's the same

MD5 How to crack websites , There is a dictionary behind them ,MD5 Encrypted value , Encrypted pre value

-- =============  test MD5  encryption  =============
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

--  Plaintext password 
INSERT INTO testmd5 VALUES(1,'zhangsan','123456'),(2,'lisi','123456'),(3,'wangwu','123456')

--  encryption 
UPDATE testmd5 SET pwd=MD5(pwd) WHERE id=1
UPDATE testmd5 SET pwd=MD5(pwd) --  Encrypt all passwords 

--  Encrypt when inserting 
INSERT INTO testmd5 VALUES(4,'xiaoming',MD5('123456'))

--  How to check : The password passed in by the user , Conduct md5 encryption , Then compare the encrypted values 
SELECT * FROM testmd5 WHERE `name`='xiaoming' AND pwd=MD5('123456')
原网站

版权声明
本文为[Mr.Rop]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180503012846.html