当前位置:网站首页>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
边栏推荐
- Timescaledb 2.5.2 release, time series database based on PostgreSQL
- Abacus mental arithmetic test
- Practical case of SQL optimization: speed up your database
- Pytorch common code snippet collection
- I use these six code comparison tools
- How to build a technical team that will bring down the company?
- WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
- A tab Sina navigation bar
- Pytorch fine tuning (Fortune): hollowed out design or cheating
- Vulnstack3
猜你喜欢
Grub 2.12 will be released this year to continue to improve boot security
Android advanced interview question record in 2022
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
丸子百度小程序详细配置教程,审核通过。
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
Restful fast request 2022.2.1 release, support curl import
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
PowerShell:在代理服务器后面使用 PowerShell
Binary tree traversal - middle order traversal (golang)
Open source SPL optimized report application coping endlessly
随机推荐
Learn tla+ (XII) -- functions through examples
He was laid off.. 39 year old Ali P9, saved 150million
[download white paper] does your customer relationship management (CRM) really "manage" customers?
runc hang 导致 Kubernetes 节点 NotReady
Variables in postman
Video display and hiding of imitation tudou.com
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Grub 2.12 will be released this year to continue to improve boot security
MATLB | multi micro grid and distributed energy trading
Visual studio 2019 set transparent background (fool teaching)
Exploration of short text analysis in the field of medical and health (II)
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Talk about the things that must be paid attention to when interviewing programmers
Redis' hyperloglog as a powerful tool for active user statistics
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Win:将一般用户添加到 Local Admins 组中
Comment mettre en place une équipe technique pour détruire l'entreprise?