当前位置:网站首页>Single line function*
Single line function*
2022-07-05 02:20:00 【Piglet get】
Catalog
Single line functions can be nested
2. Angle and radian exchange function
1. test 1 :ASCII CHAR_LENGTH LENGTH
Four 、 Date and time functions
2. Date and time stamp conversion
3. Get month 、 week 、 Weeks 、 Functions such as days
EXTRACT(type FROM date) Function type The value and meaning of :
5. Time and second conversion function
6. A function that calculates the date and time
7. Formatting and parsing of dates
① Above non GET_FORMAT Function fmt Common format characters for parameters :
②GET_FORMAT Function date_type and format_type The parameter values are as follows :
(2) analysis : The reverse of formatting
5、 ... and 、 Process control functions
6、 ... and 、 Encryption and decryption functions
7、 ... and 、 MySQL Information functions
8、 ... and 、 Other functions
One 、 What is a function ?
Function runs through the use of computer language , What does the function do ?
It can Encapsulate the code we often use , Call directly when necessary . This is both Improved code efficiency , also Improved maintainability . stay SQL We can also Use functions to perform function operations on the retrieved data . Use these functions , Can greatly Improve the efficiency of user management of database .
Classification of functions
From the perspective of function definition , We can divide the function into Built in functions and Custom function . stay SQL In language , It also includes built-in functions and custom functions .
Built in function is a general function built in the system , The custom function is written according to our own needs
Two kinds of SQL function

One line function
- Manipulate data objects
- Accept parameters and return a result
- Change only one line
- Each line returns a result
- Can be nested
- The parameter can be a column or a value
Two 、 Numerical function
1. Basic functions

SELECT ABS(-123),ABS(32),SIGN(43),PI(),CEIL(32.32),CEILING(-32.32),FLOOR(32.32)
FROM DUAL;
SELECT RAND(),RAND(),RAND(10),RAND(10),RAND(-1),RAND(-1)
FROM DUAL;
SELECT
ROUND(12.33),ROUND(12.343,2),ROUND(12.324,-1),TRUNCATE(12.66,1),TRUNCATE(12.66,-1)
FROM DUAL;

Single line functions can be nested
SELECT TRUNCATE(ROUND(123.456,2),0)
FROM DUAL;2. Angle and radian exchange function

SELECT RADIANS(30),RADIANS(60),RADIANS(90),
DEGREES(2*PI()),DEGREES(RADIANS(90))
FROM DUAL;
3. Trigonometric functions
SELECT SIN(RADIANS(30)),DEGREES(ASIN(1)),TAN(RADIANS(45)),DEGREES(ATAN(1))
FROM DUAL;
4. Exponents and logarithms
① Index
SELECT POW(2,5),POW(2,-1),POW(4,-2),EXP(2)
FROM DUAL;
② logarithm
SELECT LN(EXP(2))
FROM DUAL;
5. Conversion between bases

SELECT BIN(10),HEX(10),OCT(10),CONV(10,10,8)
FROM DUAL;
3、 ... and 、 String function



Be careful :MySQL in , The position of the string is from 1 At the beginning .
1. test 1 :ASCII CHAR_LENGTH LENGTH
SELECT ASCII('Abcvbn'),CHAR_LENGTH('HELLO'),CHAR_LENGTH(' Hello '),LENGTH('HELLO'),LENGTH(' Hello ')
FROM DUAL;
2. test 2:CONCAT CONCAT_WS
SELECT CONCAT(e.last_name,' welcome ',' to ',l.city)
FROM employees e JOIN locations l;SELECT CONCAT_WS('-','HELLO','WORLD','HELLO','XI\'AN')
FROM DUAL;
3. test 3 :INSERT,REPLACE
# The string HELLOWORLD From 2 Position start ,3 Substrings of characters long are replaced with strings aaaaa
SELECT INSERT('HELLOWORLD',2,3,'aaaaa')
FROM DUAL;SELECT REPLACE('hello','ll','mmm')
FROM DUAL;
Four 、 Date and time functions
1. Get date 、 Time

SELECT CURDATE(),CURRENT_DATE(),CURTIME(),NOW(),SYSDATE(),
UTC_DATE(),UTC_TIME()
FROM DUAL;
2. Date and time stamp conversion
①UNIX_TIMESTAMP()
With UNIX Returns the current time in the form of a timestamp .
SELECT UNIX_TIMESTAMP()
FROM DUAL;
②UNIX_TIMESTAMP
Time date With UNIX Return in the form of a timestamp .
SELECT UNIX_TIMESTAMP('2022-2-22 22:22:22')
FROM DUAL;
③FROM_UNIXTIME
take UNIX The time of the timestamp is converted to the time of the normal format
SELECT FROM_UNIXTIME(1644726483)
FROM DUAL;
3. Get month 、 week 、 Weeks 、 Functions such as days
SELECT YEAR(CURDATE()),MONTH(CURDATE()),DAY(CURDATE()),
HOUR(CURTIME()),MINUTE(NOW()),SECOND(SYSDATE())
FROM DUAL;
SELECT MONTHNAME('2022-2-22'),DAYNAME('2022-2-22'),WEEKDAY('2022-2-22'),
QUARTER(CURDATE()),WEEK(CURDATE()),DAYOFYEAR(NOW()),
DAYOFMONTH(NOW()),DAYOFWEEK(NOW())
FROM DUAL;
4. Operation function of date
EXTRACT(type FROM date) Function type The value and meaning of :
SELECT EXTRACT(SECOND FROM NOW()),EXTRACT(DAY FROM NOW()),
EXTRACT(HOUR_MINUTE FROM NOW()),#1244:12:44
EXTRACT(QUARTER FROM NOW()) # Get Quarterly
FROM DUAL;
5. Time and second conversion function
SELECT TIME_TO_SEC(CURTIME()),
SEC_TO_TIME(46066)
FROM DUAL;
6. A function that calculates the date and time
The first ① Group

SELECT NOW(),DATE_ADD(NOW(),INTERVAL 1 YEAR)# Add one year to the current time
FROM DUAL;

SELECT DATE_ADD(NOW(),INTERVAL 1 DAY) AS col1,DATE_ADD('2022-2-22 22:22:22',INTERVAL 1 SECOND) AS col2,
ADDDATE('2022-2-22 22:22:22',INTERVAL 1 SECOND) AS col3,
DATE_ADD('2022-2-22 22:22:22',INTERVAL '1_1' MINUTE_SECOND) AS col4,
DATE_ADD(NOW(), INTERVAL -1 YEAR) AS col5, # It could be negative
DATE_ADD(NOW(), INTERVAL '1_1' YEAR_MONTH) AS col6 # Need single quotes
FROM DUAL;

SELECT DATE_SUB('2022-2-22',INTERVAL 31 DAY) AS col1,
SUBDATE('2022-2-22',INTERVAL 31 DAY) AS col2,
DATE_SUB('2022-2-22 22:22:22',INTERVAL '1 1' DAY_HOUR) AS col3
FROM DUAL;
The first ② Group
SELECT
ADDTIME(NOW(),20),SUBTIME(NOW(),30),
SUBTIME(NOW(),'1:1:3'),
DATEDIFF(NOW(),'2022-2-22'),
TIMEDIFF(NOW(),'2022-2-22 22:22:22'),
FROM_DAYS(366),TO_DAYS('0000-12-25'),
LAST_DAY(NOW()),MAKEDATE(YEAR(NOW()),12),
MAKETIME(10,21,23),
PERIOD_ADD(20200101010101,10)
FROM DUAL;
7. Formatting and parsing of dates
- format : date ————> character string
- Explain Analysis : character string ————> date
At this point, we are talking about the explicit formatting and parsing of dates , What I have been exposed to before is the implicit formatting or parsing of dates
SELECT * FROM employees WHERE hire_date = '1993-01-13';' ' The writing method in must be the same as the original date format to find , The date cannot be reversed

① Above non GET_FORMAT Function fmt Common format characters for parameters :

②GET_FORMAT Function date_type and format_type The parameter values are as follows :

(1) format
SELECT
DATE_FORMAT(CURDATE(),'%Y-%M-%D') AS DATE1,
DATE_FORMAT(NOW(),'%Y-%m-%d') AS DATE2,
TIME_FORMAT(CURTIME(),'%H:%i:%S') AS DATE3,
TIME_FORMAT(CURTIME(),'%h:%i:%S') AS DATE4,
DATE_FORMAT(NOW(),'%Y-%M-%D %h:%i:%S %W %w %T %r') AS DATE5
FROM DUAL;
(2) analysis : The reverse of formatting
SELECT STR_TO_DATE('2022-February-13th 03:11:05 Sunday 0 15:11:05',
'%Y-%M-%D %h:%i:%S %W %w') AS RESULT
FROM DUAL;
SELECT GET_FORMAT(DATE, 'USA')
FROM DUAL;
SELECT DATE_FORMAT(CURDATE(),GET_FORMAT(DATE, 'USA'))
FROM DUAL;
5、 ... and 、 Process control functions
Process processing functions can be based on different conditions , Perform different processes , Can be in SQL Statement to implement different conditional choices .
MySQL The process processing functions in mainly include IF()、IFNULL() and CASE() function .

# example 1
SELECT last_name,salary,IF(salary > 6000,' High wages ',' Low wages ') "details"
FROM employees;
# example 2
SELECT employee_id,salary, CASE WHEN salary>=15000 THEN ' High salaries '
WHEN salary>=10000 THEN ' potential share '
WHEN salary>=8000 THEN ' Prick silk '
ELSE ' grassroots ' END " describe "
FROM employees;
# example 3
SELECT oid,`status`, CASE `status` WHEN 1 THEN ' Unpaid '
WHEN 2 THEN ' Paid '
WHEN 3 THEN ' Shipped '
WHEN 4 THEN ' Confirm receipt '
ELSE ' Invalid order ' END
FROM t_order;
6、 ... and 、 Encryption and decryption functions
The encryption and decryption function is mainly used to encrypt and decrypt the data in the database , To prevent data from being stolen by others . These functions are very useful in ensuring database security .
You can see ENCODE(value,password_seed) Function and DECODE(value,password_seed) Functions are inverse functions to each other .
SELECT PASSWORD('MySQL') #PASSWORD stay MySQL 8.0 To discard
FROM DUAL;
SELECT MD5('MySQL'),SHA('MySQL')
FROM DUAL; 
notes :PASSWORD & SHA Encryption is irreversible
7、 ... and 、 MySQL Information functions
MySQL There are some built-in functions that can be queried MySQL Functions of information , These functions are mainly used for Help database development or operation and maintenance personnel better maintain the database .

SELECT VERSION(),# Version number
CONNECTION_ID(),# Connect ID
DATABASE(),SCHEMA(),USER(),CURRENT_USER(),
CHARSET(' China '),COLLATION(' China ')
FROM DUAL;
8、 ... and 、 Other functions

边栏推荐
- How to find hot projects in 2022? Dena community project progress follow-up, there is always a dish for you (1)
- Comment mettre en place une équipe technique pour détruire l'entreprise?
- The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
- "C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
- STL container
- Pytorch fine tuning (Fortune): hollowed out design or cheating
- Open source SPL optimized report application coping endlessly
- openresty ngx_lua執行階段
- Tla+ through examples (XI) -- propositional logic and examples
- Some query constructors in laravel (2)
猜你喜欢

He was laid off.. 39 year old Ali P9, saved 150million

力扣剑指offer——二叉树篇

Restful fast request 2022.2.1 release, support curl import

Chinese natural language processing, medical, legal and other public data sets, sorting and sharing

. Net starts again happy 20th birthday

Application and Optimization Practice of redis in vivo push platform

Official announcement! The third cloud native programming challenge is officially launched!

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

Win:使用 Shadow Mode 查看远程用户的桌面会话

Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
随机推荐
One plus six brushes into Kali nethunter
. Net starts again happy 20th birthday
Three properties that a good homomorphic encryption should satisfy
Traditional chips and AI chips
RichView TRVStyle MainRVStyle
Win:将一般用户添加到 Local Admins 组中
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
Redis distributed lock, lock code logic
Runc hang causes the kubernetes node notready
Erreur de type de datagramme MySQL en utilisant Druid
Marubeni Baidu applet detailed configuration tutorial, approved.
官宣!第三届云原生编程挑战赛正式启动!
Introduce reflow & repaint, and how to optimize it?
Do you know the eight signs of a team becoming agile?
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Grpc message sending of vertx
Tla+ through examples (XI) -- propositional logic and examples
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Uniapp navigateto jump failure
Introduce reflow & repaint, and how to optimize it?









