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: 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 6: Not in the range 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 (>=) *********************************************************************************************************************** Mathematical functions 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 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() 4: min() 5: max() *********************************************************************************************************************** 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 3: Get the current time 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 6: Calculate date difference 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 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')); String functions 1: left(str, len) returns len characters from the left end of the string str 2: length() 3: lower(str) returns the lowercase string str 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 5: concat() string concatenation 6: replace You may also be interested in:
|
<<: How to count the number of specific characters in a file in Linux
>>: Two-hour introductory Docker tutorial
Structure related tags ---------------------------...
background Recently, a leader wanted us to build ...
MySQL8.0.22 installation and configuration (super...
The effect is as follows:Reference Program: <!...
Table of contents 1. RegExp object 2. Grammar 2.1...
Version Chain In InnoDB engine tables, there are ...
The data dictionary in MySQL is one of the import...
Table of contents Preface Installation and Usage ...
Vue - implement the shuttle box function, the eff...
HTML comment box with emoticons. The emoticons ar...
Table of contents 1. Automatic installation using...
Preface Mobile devices have higher requirements f...
Table of contents Preface Parsing parameters Modi...
This article records the detailed tutorial of MyS...
In a recent project, I wanted to align text verti...