[MySQL] built-in function

foreword

        hi~ Welcome to my MySQL notes series! This article mainly records the process of learning how to use functions to process data in MySQL database.

My mysql note directory is as follows~

[MySQL] Add, delete, check and modify tables - qihaila's blog - CSDN blog

[MySQL] Table Constraints

[MySQL] table operations and data types

[MySQL] library operation

Table of contents

1. Date function

- get date

-Get Time

- get timestamp

- Date operations

-Case: Create a message form

2. String functions

- Get the character set of the ename column of the emp table

- Connect each employee information to table emp

substring and case conversion

Find the number of bytes occupied by a string

replace string

Compare the size of two strings

-Display the name of the employee in the emp table with the first letter capitalized and the rest in lowercase

remove spaces

3. Mathematical functions

4. Other functions


1. Date function

        The date function is a series of date operations provided in the mysql database, including date types (date, datetime, timestamp).

- get date

        Get the current year, month, and day:

select current_date();

        Use the date function to filter the date by getting the date time:

select date(now());

-Get Time

        Get the current hour, minute and second:

select current_time();

- get timestamp

        Get the current timestamp (the timestamp type is displayed as the datetime type by default in the select query):

select current_timestamp();

 

- Date operations

        On the basis of a date, perform different types of operations: ( day-day, second-second, minute-minute, hour-hour, year-year ). Among them, date_add is plus, and date_sub is minus.

         It should be noted that date can be of date or datetime type. If the date type performs time type operations on it (second, minute, hour), it will be converted to datetime type for operations from 00:00:00.

        Don't forget to write the interval keyword.

        The unit calculated by datediff(date1, date2) is day. If the parameter passed is datetime type, it will be converted to date type.

-Case: Create a message form

        According to the date function above, we create a message table to count attributes such as the time of the messager.

create table if not exists message(
    id int unsigned auto_increment primary key,
    content varchar(50) not null,
    sendtime datetime
);

         Insert a few pieces of data:

         Now we display all message information, and the release date only shows the date, not the time:

         Now insert some data, and display the comments posted in the last minute:

select * from message where date_sub(now(), interval 1 minute) < sendtime;

2. String functions

        In MySQL, some functions that operate on strings are provided, as follows:

        Below, we use the Oracle 9i classic test table for testing. (The table scott_data.sql  can be imported into the test database by executing source)

- Get the character set of the ename column of the emp table

        Obtaining the character set is also the encoding format.

select charset(ename) from emp;

 

         In addition to strings, you can also view numeric types, and the encoding format is binary.

         The charset function is mainly used to view the encoding format of the corresponding string. If there are garbled characters in the select query, it must be that the encoding format does not correspond to the verification rules, so you can use this function to check.

- Connect each employee information to table emp

        The employee information of each record is expressed as follows: xxx employee Congratulations on joining our company at xxx position, the employment time is xxx salary is xxx, and the department number is xxx.

        We can concatenate each string using concat.

select concat(ename, '员工恭喜你入职我司', job, '职位,雇佣时间为', date(hiredate), '工资为', sal, ',部门编号是', deptno) from emp;

         It can be found that non-str types can also be connected and nested.

substring and case conversion

        The first is to find the substring. Use instr (str1, str2) to find the position of str2 in str1. Note that the location starts with a subscript of 1, and returns 0 if it is not found.

         The unit you are looking for is character, not byte~

        In addition, we can extract n characters from the left to form a substring (left(str, n)), or we can extract n characters from the right to form a string (right(str, n));

        We can use ucase and lcase to convert lowercase characters in a string to uppercase, and uppercase characters to lowercase.

        For example, we display the names of all employees in the emp table in lowercase:

Find the number of bytes occupied by a string

        The length can be calculated by using the function length, but it should be noted that the unit is byte.

        - For example, find the number of bytes occupied by the following two strings: (utf-8 Chinese characters occupy 3 bytes)

replace string

Use the function replace(str, str1, str2); to replace all str1 places         in str with str2 strings.

Compare the size of two strings

        Use the function strcmp(str1, str2) to compare the size of the two substrings of str1 and str2 (dictionary comparison). Returns 1 if str1 > str2, -1 if str1 < str2, 0 if equal.

 

-Display the name of the employee in the emp table with the first letter capitalized and the rest in lowercase

        If you want to achieve the requirements of the topic, you should divide the name into two parts, the first part is the first letter, and the second part is the latter part. And this matter can be intercepted using the function substring(str, b[, n]). Starting from b, the interception is completed by default, otherwise n characters are intercepted to form a new string. (Note that the first character subscript is 1)

select concat(ucase(left(ename, 1)), lcase(substring(ename, 2))) from emp;

remove spaces

        In some scenarios, such as bash, there are some useless spaces in the front or back of the effective string, which need to be removed when used (remove unnecessary spaces from the prefix and suffix of a string, and the spaces in the middle of the string will not was removed ):

         We can use ltrim to remove the spaces on the left, rtrim to remove the right, and trim to remove all.

 

3. Mathematical functions

        Note that rounding up is rounding towards positive infinity and rounding down is rounding towards negative infinity. mod Negative modulo is also possible.

4. Other functions

function effect
user(); Query the current user
md5(str); Perform a character digest on str, which is a 32-bit string
database(); Show the database currently in use
password(str); Similar to md5, except with a 41-bit digest with *
ifnull(val1, val2); If val1 is null, return val2, otherwise return val1.

Guess you like

Origin blog.csdn.net/weixin_61508423/article/details/130340465