A brief introduction to MySQL functions

A brief introduction to MySQL functions

The main MySQL functions are introduced as follows:

  • Mathematical functions
  • String functions
  • Time function
  • Encryption Function

1. Mathematical functions

Note: Each function needs to be preceded by: SELECT .

Mathematical functions

  • ABS() returns the absolute value such as: (-100) Value: 100
  • PI() returns the compass ratio of π (no need to write): 3.1415926
  • CEIL() rounds up to an integer, such as (3.14) Value: 4 (note the third digit is i)
  • FLOOR() rounds down to an integer, such as: (3.14) Value: 3
  • POW(x,y) x to the power of y, such as (2,3) Value: 8
  • RAND() returns a random value between 0 and 1, such as: () Value: 0.018137501569592863
  • TRUNCATE(x,y) x retains y decimal places, such as: (3.1415926,3) value: 3.141
-- abs() absolute value SELECT ABS (-100);

-- pi() returns the regularity of a circle SELECT PI();

-- SQRT() returns the square of a non-negative number x SELECT SQRT(2);

-- POW returns x raised to the power of y. SELECT POW(2,10);
SELECT POWER(2,10)

-- CELL() or 
SELECT CEIL(3.14); -- round up to the nearest integer -- floor()
SELECT FLOOR(3.14); -- Round down to an integer -- round() can round to an integer and also keep decimals SELECT ROUND(3.4);
SELECT ROUND(3.5);
SELECT ROUND(3.7,2)


-- pow() x to the power of y SELECT POW(2,3);


-- random Random 0 to 1 
SELECT RAND();

-- truncate() retains decimals SELECT TRUNCATE(3.14159265758,3);
SELECT TRUNCATE(RAND()* 1000 ,3);

2. String functions

String functions

  • LENGTH() gets the length, such as: ('abc') The value is: 3
  • CHAR_LENGTH length such as: ('add') value: 3
  • UPPER() English characters in uppercase letters ('abcde') in the string: ABCDE
  • LOWER() All English characters in the string are lowercase ()
  • TRIM() removes spaces
  • REVERES()
  • REPLACE(str,len1,len2) len1 replaces len2
  • SUBSTRLNG(y,z) intercepts y to z
-- String function -- RIGHT

SELECT LENGTH('fdsajfadslksdafk');

SELECT CHAR_LENGTH('fdsajfadslksdafk');

-- lower upper --Change all to uppercase and lowercase SELECT UPPER('fdsajfadslksdafk');
SELECT LOWER('AFASDF');

-- trim() removes spaces at both ends SELECT TRIM(' abc ' )
SELECT TRIM('afdjkadsfjkll asd;fasf ');

-- reverse() reverse SELECT REVERSE('abcdefg');
SELECT REVERSE(TRIM('afdjkadsfjkll asd;fasf '));、


-- replace(str,len1,len2) len1 replaces len2
SELECT REPLACE('Everyone in the QQ group is talented and speaks well', 'Talent', 'Genius');

SELECT REPLACE('路展其是班草', '阿呆呆', '曹某某');

-- Intercept y Intercept z 
SELECT SUBSTRING('Everyone in prison is talented and speaks well',4,5);

3. Date functions

Date functions

  • SYSDATE() system date + time
  • CURDATE() system date
  • CURETIME() system time
  • WEEKDAY() returns the date parameter. Note that time: 0-6 needs to be +1
  • DAYNAME() system tells you what day of the week it is
  • YEAR() to find the year
  • MONTH() to check the month
  • DAY() to check the date
  • HOUR() to check the time
  • MINUTE() to check the score
  • SECOUND() to check seconds
  • WEEK() to check the week

ADDDATE ( date , interval , value) adds to the following date, such as: (NOW(),INTERVAL,50 YEAR)

DATEDIFF ( date , date ) separates two values, such as: ABS (DATEDIFF('2005-8-26','2021-9-23'))

-- Date function -- System current date sysdate()
SELECT SYSDATE() System current date; -- Hours, minutes, seconds -- (date and time)
SELECT CURDATE() System current date; -- Current date -- (date)
SELECT CURTIME() system current time;
-- (time)

-- dayofweek() Today is week - month - year
SELECT DAYOFYEAR(SYSDATE()) Today is the day of the year;
SELECT DAYOFMONTH(SYSDATE()) Today is the first day of this month; 
SELECT DAYOFWEEK('2021-9-19') What day of the week is today? -- 1-7

--weekday 
SELECT WEEKDAY(SYSDATE()) + 1 What day is today? -- 0-6

-- dayname day of the week SELECT DAYNAME('2001-9-18') 

-- year month day hour minute second
SELECT YEAR(SYSDATE()) YEAR;
SELECT MONTH(SYSDATE()) month;
SELECT DAY(SYSDATE()) DAY;

SELECT HOUR(SYSDATE());
SELECT MINUTE(SYSDATE()) MINUTE;
SELECT SECOND(SYSDATE()) seconds; 
-- Week SELECT WEEK(SYSDATE()) week; 


-- adddate(date,interval expr type) adds time to the date -- Assuming you have 50 years left to live, what will the time be in 50 years? SELECT ADDDATE(NOW(),INTERVAL 50 YEAR);
SELECT ADDDATE(NOW(), INTERVAL 50 SECOND);

-- datediff(date1, date2) The interval between two dates SELECT ABS(DATEDIFF('2005-8-26','2021-9-23')); -- 1984 - 3 - 24


-- Assuming that he was born on July 15, 2005, how many minutes did he live? -- Assuming that he has 80 years to live, how long will it be after 80 years? SELECT ABS(DATEDIFF('2005-7-15',SYSDATE()) ) * 1444;

SELECT ADDDATE(NOW(), INTERVAL 80 YEAR);

4. Encryption Function

  • PASSWORD(str) cannot be reversed after encryption
  • MD5(str) encryption can be reversed Note: Previously not allowed
  • CHARSET() to view mysel version
-- Encryption function SELECT PASSWORD('123456') cannot be reversed after encryption;
SELECT PASSWORD('666666') Encryption cannot be reversed;

--MD5
SELECT PASSWORD('123456') cannot be reversed after encryption MD5('123456') can be reversed after encryption -- View the version SELECT VERSION();
SELECT CHARSET('123456');


-- md5 encryption -- Snowflake Twitter algorithm

This is the end of this article about MySQL functions. For more relevant MySQL function content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL detailed summary of commonly used functions
  • Comprehensive summary of mysql functions
  • mysql calculation function details
  • Introduction to commonly used functions in MYSQL database

<<:  Detailed explanation of JQuery selector

>>:  A brief introduction to the differences between HTML and XHTML, and HTML4 and HTML5 tags

Recommend

Detailed usage of Vue more filter widget

This article example shares the implementation me...

MySQL 5.7.18 free installation version configuration tutorial

MySQL 5.7.18 free installation version installati...

Idea deploys remote Docker and configures the file

1. Modify the Linux server docker configuration f...

Specific example of MySQL multi-table query

1. Use the SELECT clause to query multiple tables...

How to use Vue to develop public account web pages

Table of contents Project Background start Create...

An article to help you understand jQuery animation

Table of contents 1. Control the display and hidi...

Detailed explanation of how to use several timers in CocosCreator

1. setTimeOut Print abc after 3 seconds. Execute ...

Docker image analysis tool dive principle analysis

Today I recommend such an open source tool for ex...