Summary of commonly used operators and functions in MySQL

Summary of commonly used operators and functions in MySQL

Let’s build the data table first.

use test;
create table `employee`(
 emp_no int unsigned,
 emp_name varchar(30),
 emp_sex varchar(3),
 emp_age tinyint unsigned,
 sal double,
 history datetime
);
insert into employee values(1, '张三', '男', 18, 5000, '2012-04-23'),
(2, 'Li Si', 'Male', 27, 4500, '2013-05-23'),
(3, '王五', '男', 23, 4700, '2012-04-21'),
(4, 'Zilong', 'Male', 19, 3800, '2011-03-04'),
(5, 'Li Bai', 'Male', 15, 6200, '2015-09-09'),
(6, 'Liu Bei', 'Male', 28, 2500, '2016-02-11'),
(7, 'Lu Bu', 'Male', 21, 6000, '2010-10-18'),
(8, 'Shangxiang', 'female', 16, 4500, '2011-09-26'),
(9, 'Xiao Qiao', 'Female', 15, null, '2013-07-05'),
(10, '大乔', '女', 16, 5000, '2017-09-01');

Commonly used operators:
1: equal to ( = )

 select * from employee where sal = 3800;
 select * from employee where sal = null; --No null data can be found here

2: Equal to ( <=> )

 select * from employee where sal <=> 3800;
 select * from employee where sal <=> null; --Here you can query the data that is null

3: is judgment (null)

 select * from employee where sal is null;
 select * from employee where sal is not null;

4: You can also use isnull() to judge null value;

 select * from employee where isnull(sal);
 select * from employee where !isnull(sal);

5: between min and max ps: this is a closed interval

select * from employee where sal between 4500 and 5000;

6: Not in the range

select * from employee where sal not between 4500 and 5000; included

7: and and or

 select * from employee where sal not between 4500 and 5000 or sal is null;
 select * from employee where sal = 4500 and emp_sex = '女';

8: less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=)

select * from employee where sal >= 4500;

***********************************************************************************************************************

Mathematical functions
1: rand();

 select rand() from dual; --dual is a pseudo table select 1+1 from dual;
 select rand(); --can be abbreviated

2: least(value1, value2, ...) returns the minimum value

 select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
 select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76) as min_value; --The column name can have an alias

3: greatest(value1, value2, ...) returns the greatest value

select greatest(54,76,4,65,76,87,87,56,65,654,45,23,1,76);

4: round(M, D); Returns the rounded value of M, D indicates how many decimal places to keep, the default value is 0

 select round(1.69);
 select round(1.69, 1);

5: abs() absolute value

 select 5-10;
 select abs(5-10);

***********************************************************************************************************************

Summary functions

1: avg();

 select * from employee where sal >= 6000;
 select avg(sal) from employee where sal >= 6000;

2: count()

 select count(*) from employee;
 select count(emp_name) from employee;
 select count(sal) from employee; --print 9. Null values ​​will be ignored here. select count(*) from employee where sal >= 4000;
 select count(*) from employee where sal <= 4000 or sal is null;

3: sum()

select sum(sal) from employee where sal >= 6000;

4: min()

select min(sal) from employee;

5: max()

select max(sal) from employee;

***********************************************************************************************************************

Date functions

1: Get the current date and time

 select now(), sysdate(), current_timestamp();
 select now(6), sysdate(6), current_timestamp(6);
 ps: now(), current_timestamp(); no difference, indicates the time when sql starts to execute sysdate() indicates the start time of this function

2: Get the current date

select curdate(); --Only year, month and day

3: Get the current time

select curtime(); --only hours, minutes and seconds

4: Date addition operation date_add

 select history, date_add(history, interval '1 12:10' day_minute) from employee; --date_add(history, interval '1 12:10' day_minute)
 select history, date_add(history, interval '1-1' year_month) from employee; --date_add(history, interval '1-1' year_month)
 select history, date_add(history, interval '1' second) from employee; --date_add(history, interval '1' second)

5: Date subtraction operation data_sub

select history, date_sub(history, interval '1-1' year_month) from employee;

6: Calculate date difference

select history, sysdate(), datediff(sysdate(), history) from employee; --以天數來表示

7: Get the specified part of the date (convert the date to the specified format) date_format()

 select history, date_format(history, '%Y year %m month %d number') from employee;
 select history, date_format(history, '%d号') from employee;
 select history, date_format(history, '%Y year %m month %d date %H hour %i minute %s second') from employee;

8: Calculate the day of the week for a date

select history, dayname(history) from employee;

9: Chinese date string conversion date str_to_date()

 insert into employee values(11, '张飞', '男', 22, 3000, '2017年02月01号'); --Error insert into employee values(11, '张飞', '男', 22, 3000, str_to_date('2017年02月01号', '%Y年%m月%d号%H时%i分钟%s秒'));

insert into employee values(12, 'Second brother', 'Male', 22, 3000, str_to_date('2017-02-01 23:02:02', '%Y year %m month %d date %H hour %i minute %s second'));
insert into employee values(12, 'Second brother', 'Male', 22, 3000, str_to_date('2017-02-01 11:02:02', '%Y year %m month %d date %h hour %i minute %s second'));
ps: If it is h, it means 12-hour system, if it is big H, it means 24-hour system;

String functions

1: left(str, len) returns len characters from the left end of the string str

select left('abcdefg', 5);

2: length()

select length('abcdefg');

3: lower(str) returns the lowercase string str

select lower('HELLO');

4: substring() takes a substring, the second parameter is the starting position of the interception, and the third parameter is the length to be intercepted

select substring('helloworld',2,3);

5: concat() string concatenation

select concat(emp_name, '員工') from employee;

6: replace

select replace(emp_name, '李', '老') from employee where emp_name = '李四';

You may also be interested in:
  • MySQL Where conditional statement introduction and operator summary
  • Examples of operators used in MySQL
  • MySQL Notes: Detailed Explanation of Operator Usage
  • Study on the default rules of mySQL UNION operator
  • Learning MYSQL in comparison with MSSQL (V) - Operators
  • Summary of the use of special operators in MySql
  • Teach you MySQL operators step by step

<<:  How to count the number of specific characters in a file in Linux

>>:  Two-hour introductory Docker tutorial

Recommend

WML tag summary

Structure related tags ---------------------------...

50 lines of code to implement Webpack component usage statistics

background Recently, a leader wanted us to build ...

MySQL 8.0.22 installation and configuration graphic tutorial

MySQL8.0.22 installation and configuration (super...

Write a simple calculator using JavaScript

The effect is as follows:Reference Program: <!...

A brief discussion on JS regular RegExp object

Table of contents 1. RegExp object 2. Grammar 2.1...

A Brief Analysis of MySQL - MVCC

Version Chain In InnoDB engine tables, there are ...

Detailed explanation of MySQL 8.0 dictionary table enhancement

The data dictionary in MySQL is one of the import...

vue-cli configuration uses Vuex's full process record

Table of contents Preface Installation and Usage ...

Vue's detailed code for implementing the shuttle box function

Vue - implement the shuttle box function, the eff...

Tutorial on how to create a comment box with emoticons using HTML and CSS

HTML comment box with emoticons. The emoticons ar...

How to install and use Ubuntu Docker

Table of contents 1. Automatic installation using...

Some wonderful uses of URL objects in JavaScript

Table of contents Preface Parsing parameters Modi...

How to install MySQL Community Server 5.6.39

This article records the detailed tutorial of MyS...

How to realize vertical arrangement of text using CSS3

In a recent project, I wanted to align text verti...