MySQL basics (5)-commonly used functions in MySQL

Preface

  The last article introduced you to multi-table join queries and nested queries , including inner joins and outer joins, and also introduced the usage of sub-queries, including keywords such as in and any. Finally, the knowledge points of this part are explained through cases. This article introduces you to some commonly used functions in MySQL, which mainly include mathematical functions, string functions, date and time functions, grouping and merging functions, logical functions, and windowing functions. But the window function is very important, we will introduce it separately in the next article. First, I will introduce string functions to you.

One, string function

  We first introduce the related functions of string functions through a table:

  these are probably the string functions we commonly use. In this article, we only introduce the most common string functions. If you use it in your work, you can check the related usage;
1. CONCAT(str1,str2,...)Combine multiple text strings into one long string; the specific usage is as follows :

select concat('我们','要', '好好学习');

  The results of the execution are as follows:
  What we need to pay attention to here is the NULLvalue problem. Any connection between a string and a NULL value is NULL, as follows:

select concat('我们',NULL, '好好学习');

  The results of the execution are as follows:

2. INSTR(str,substr)Return the position of the first occurrence of the substring substr in the text string str (), the specific usage is as follows:

select instr('我们要学好MySQL', '学好');

  The results of the execution are as follows:

3. LEFT(str,len)Return len characters from the left end of the string str. The specific usage is as follows:

select left('我们要学好MySQL', 3);

  The results of the execution are as follows:

4. RIGHT(str,len)Return len characters from the right end of the string str. The specific usage is as follows:

select right('我们要学好MySQL', 3);

  The results of the execution are as follows:

5. MID(str,pos,len)Return len characters from the position pos of the string str. The specific usage is as follows:

select mid('我们要学好MySQL', 4,2);

  The results of the execution are as follows:

6. SUBSTRING ( expression, start, length )Intercept the string, and the parameters are as follows:

  • expression : string, binary string, text, image, column, or expression containing column. Do not use expressions that include aggregate functions.
  • start : An integer or an expression that can be implicitly converted to an int, specifying the starting position of the substring.
  • length : Integer or an expression that can be implicitly converted to int, specifying the length of the substring.

  The specific usage is as follows:

select substring('我们要学好MySQL',1,3);

  The results of the execution are as follows:

7. REPLACE(str,from_str,to_str)Replace the substring from_str in the string str with the string to_str and return. The specific usage is as follows:

select replace('我们要学好MySQL', 'MySQL', 'JAVA');

  The results of the execution are as follows:

8. REPEAT(str,count)Return a string composed of count strings str. The specific usage is as follows:

select repeat('我们要学好MySQL', 3);

  The results of the execution are as follows:

9. UPPER(str)Return the uppercase string str, the specific usage is as follows:

select upper('mysql');

  The results of the execution are as follows:

10. LOWER(str)Return the uppercase string str, the specific usage is as follows:

select lower('MYSQL');

  The result of the implementation is as follows:

  Convert the first letter of each employee's name to uppercase

select concat(upper(left(empname,1)), lower(mid(empname,2))) from emp;

  The results of the implementation are as follows:

2. Mathematical functions

  We first introduce you to the related functions of mathematical functions through a table:

  we have all mentioned before, but only introduce the most commonly used ones. This part is also the same;
1. ABS(n)Return the absolute value of n, the specific usage is as follows:

select abs(-32);

  The results of the execution are as follows:

2. FLOOR(n)Return the largest integer value not greater than n. The specific usage is as follows:

select floor(1.23);

  The results of the execution are as follows:

3. CEILING(n)Return the smallest integer value not less than n. The specific usage is as follows:

select ceiling(1.23);

  The results of the execution are as follows:

4. ROUND(n,d)Return the rounded value of n, keeping d decimal places (the default value of d is 0), the specific usage is as follows:

select round(1.58);

  The results of the execution are as follows:

5. RAND(n)Return a random floating point value in the range of 0 to 1.0 (the number n can be used as the initial value), the specific usage is as follows:

select rand();

  The results of the execution are as follows:

  but we also have parameters inside, the specific usage is as follows:

select rand(2);

  The results of the implementation are as follows:

Third, the date function

  We first introduce the related functions of the date function through a table:

  we have mentioned it before, but only introduce the most commonly used ones. This part is also the same;
1. DATE(date)Return the date part of the specified date/time expression or convert the text to a date format. The specific usage is as follows:

select date('20200101');

  The results of the execution are as follows:

2. WEEK(date)Return the week of the year when the specified date is as follows:

select week('2021-03-27');

  The results of the execution are as follows:

3. MONTH(date)Return the month of the specified date, as follows:

select month('2021-03-27');

  The results of the implementation are as follows:

4. QUARTER(date)Return the first few quarters of the year for the specified date. The specific implementation is as follows:

select quarter('2021-03-27');

  The results of the execution are as follows:

5. YEAR(date)Return the year of the specified date (range from 1000 to 9999). The specific implementation is as follows:

select year('2021-03-27');

  Implementation of the results are as follows:

  But our date function also can add and subtract, as follows: DATE_ADD(date,interval expr type), , ADDDATE(date,interval expr type), DATE_SUB(date,interval expr type), SUBDATE(date,interval expr type)Next, we introduce the meaning of the parameters:

  • date is a datetime or date value
  • expr an expression string for adding and subtracting date
  • type specifies how the expression expr should be interpreted

  Our general parameters will involve hour, minute, second, year, month, and day. The specific meaning and format are shown in the following table: The


  specific usage is as follows:

select date_add("2021-03-27",interval 1 day);

  The specific implementation results are as follows: The

  previous introduction is to add one day, we can also subtract one day, and the specific implementation is as follows:

select date_sub("2021-03-27", interval 1 day);

  The specific execution results are as follows:

6. DATE_FORMAT(date,format)Format the date value according to the format string. Markers can be used in the format string. The specific parameters are as follows:

  • %m month name (january……december)
  • %Y year, number, 4 digits
  • %y year, number, 2 digits
  • %a Abbreviated week name (sun……sat)
  • %d The number of days in the month, number (00……31)
  • %e The number of days in the month, number (0……31)
  • %m month, number (01……12)
  • %c month, number (1……12)
  • %b abbreviated month name (jan...dec)
  • %j The number of days in a year (001……366)
  • %h hour (00……23)
  • %k hours (0……23)
  • %i minute, number (00……59)
  • %r time, 12 hours (hh:mm:ss [ap]m)
  • %t time, 24 hours (hh:mm:ss)
  • %s seconds (00……59)
  • %p am or pm
  • %w The number of days in a week (0=sunday ……6=saturday)
  • %u week (0……52), where Sunday is the first day of the week
  • %% character%

  The specific usage is as follows:

select date_format('2021-03-27 15:21:00','%Y-%m-%d');

  The specific execution results are as follows:

7. CURDATE()Return the current date value in the format of'yyyy-mm-dd' or yyyymmdd (according to the context of the return value is a string or number), the specific usage is as follows:

select curdate();

  The specific execution results are as follows:

8. The CURTIME()current time value is returned in the format of'hh:mm:ss' or hhmmss (according to the context of the return value is a string or a number), the specific implementation is as follows:

select curtime();

  The specific execution results are as follows:

9. NOW()Return the current date and time in the format of'yyyy-mm-dd hh:mm:ss' or yyyymmddhhmmss (according to the context of the return value is a string or a number), the specific implementation is as follows:

select now(); 

  The specific execution results are as follows:

10. DATEDIFF(expr1,expr2)Return the number of days between the end date expr1 and the start date expr2, and the specific implementation is as follows:

-- 练习:查询每位员工的工龄:ename,hiredate,工龄
select empno,empname,job,hiredate,datediff(curdate(),hiredate)/365 工龄 from emp;

  The specific execution results are as follows:

11. UNIX_TIMESTAMP()Return a unix timestamp (the number of seconds since '1970-01-01 00:00:00', date defaults to the current time), the specific implementation is as follows:

select unix_timestamp();

  The specific implementation results are as follows:

  Of course, we can also unix_timestamp()include the date in it, as follows:

select unix_timestamp('2021-03-27');

  The specific execution results are as follows:

12. FROM_UNIXTIME(unix_timestamp)The value of the timestamp is returned in the format of'yyyy-mm-dd hh:mm:ss' or yyyymmddhhmmss (according to the context of the return value is a string or a number), the specific implementation is as follows:

select from_unixtime(1577808000);

  The specific implementation results are as follows:

  query the expiration date of each employee (the trial period is three months): empname, hiredate, trial expiration date

select empname, hiredate, adddate(hiredate, interval 3 month) 试用截止日期 from emp; 

  The specific implementation is as follows:

Fourth, the conversion function

1. It is CAST(expression AS data_type)used to explicitly convert an expression of a certain data type to another data type. The parameter of the CAST() function is an expression, which includes the source value and target data type separated by the AS keyword, the specific The parameters are as follows:

  • expression : any valid SQL expression.
  • AS : used to separate two parameters, before AS is the data to be processed, and after AS is the data type to be converted.
  • data_type : The data types provided by the target system, including bigint and sql_variant, cannot use user-defined data types.

  The types that can be converted are as follows:

  • Binary, with the effect of binary prefix: BINARY
  • Character type, with parameters: CHAR()
  • Date: DATE
  • Time: TIME
  • Date time type: DATETIME
  • Floating point number: DECIMAL
  • Integer: SIGNED
  • Unsigned integer: UNSIGNED

  The specific implementation is as follows:

select cast('9.0' AS decimal);

  The specific implementation results are as follows:

Five, group and merge function

GROUP_CANCAT([distinct] str [order by str asc/desc] [separator])Concatenate the values ​​in the same group produced by group by and return a string result. The specific usage is as follows:

-- 查询各部门的员工姓名
select deptno,group_concat(empname) from emp group by deptno;

  The specific implementation results are as follows:

Six, logic function

1. IFNULL(expression, alt_value)Determine whether the first expression is NULL, if it is NULL, return the value of the second parameter, if it is not NULL, return the value of the first parameter. The specific usage is as follows:

select ifnull(comm,0) from emp;

  The specific execution results are as follows:

2. IF(expr1,expr2,expr3)If the value of expr1 is true, the value of expr2 is returned, and if the value of expr1 is false, the value of expr3 is returned. , The specific implementation is as follows:

-- 查询每位员工的工资级别:3000及以上为高,1500-3000为中,1500及以下为低
select empname,sal,if(sal>=4000,'高',if(sal>=3500,'中','低')) 工资级别 from emp;
-- 计算每位员工的实发工资(基本工资+提成):empname,sal,实发工资 
select empname, sal, sal+ifnull(comm, 0) 实发工资 from emp; 

  The specific execution results are as follows:


3. CASE WHEN expr1 THEN expr2 [WHEN expr3 THEN expr4...ELSE expr] ENDIf the value of expr1 is true, the value of expr2 is returned; if the value of expr3 is false, the value of expr4 is returned. The specific implementation is as follows:

select empname,sal,case when sal>=3000 then '高' when sal>=1500 then '中' else '低' end 工资级别 from emp;

  The specific execution results are as follows: the

  last part is the window function, but it is more critical, we will introduce it in detail in the next article.

to sum up

  The last article introduced you to multi-table join queries and nested queries , including inner joins and outer joins, and also introduced the usage of sub-queries, including keywords such as in and any. Finally, the knowledge points of this part are explained through cases. This article introduces you to some commonly used functions in MySQL, including mathematical functions, string functions, date and time functions, grouping and merging functions, logical functions, and windowing functions. But the window function is very important, we will introduce it separately in the next article. In addition, we also introduce various usages of this function through cases. Therefore, mysql is a very important skill. Almost every job in the computer needs a mysq skill. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/115260304