Table of contents
First, the basic operation of the database
Second, the basic query of the database
1. Sorting ORDER BY default ascending order, ASC ascending order
3. Remove duplicate data DISTINCT
4. Conditional query WHERE conditional splicing AND logic and OR logic or
1. Control conversion IFNULL(expr1,expr2)
2. The difference in days between two time periods DATEDIFF(expr1,expr2)
9. Execution order of query conditional statements
3. MAX() Find the maximum value
4. MIN() Find the minimum value
5. COUNT(*) counts the number of entries
6. Precautions for aggregate functions
7, LENGHTH () statistical string length
First, the basic operation of the database
# show database
SHOW DATABASES;
#create database
CREATE DATABASE demo;
#delete database
DROP DATABASE demo;
USE demo; #切换逻辑库
#Create data table student
CREATE TABLE student ( id INT UNSIGNED PRIMARY KEY, #UNSIGNED无符号的整数,正整数 主键:无重复字段 name VARCHAR ( 20 ) NOT NULL, sex CHAR ( 1 ) NOT NULL, birthday DATE NOT NULL, tel CHAR ( 11 ) NOT NULL, #char固定长度 remark VARCHAR ( 200 ) #备注 );
# Insert a piece of data
INSERT INTO student VALUES ( 1, "李强", "男", "1995-12-20", "17788990089", NULL );
SHOW TABLES;#查看当前逻辑空间有什么表
#View the specific situation of the data table
DESC student;
#View create table statement
SHOW CREATE TABLE student;
#Generate create table SQL text
CREATE TABLE `student` ( `id` int unsigned NOT NULL, `name` varchar(20) NOT NULL, `sex` char(1) NOT NULL, `birthday` date NOT NULL, `tel` char(11) NOT NULL, `remark` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# delete data table
DROP TABLE student;
Second, the basic query of the database
1. Paging query
#Paging query: SELECT * FROM table name LIMIT starting position, offset,
The current page starts from the first piece of data, check 20 pieces of data
SELECT * FROM appuser LIMIT 0,20;
2. Sorting
1. Sorting ORDER BY default ascending order, ASC ascending order
SELECT username,rule FROM appuser ORDER BY rule ASC; #ASC升序
2, DESC descending sort
SELECT username,rule FROM appuser ORDER BY rule DESC;
3. Paging after sorting
SELECT * FROM appuser ORDER BY rule DESC,username ASC LIMIT 0,20;
3. Remove duplicate data DISTINCT
When querying multiple fields, the DISTINCT query will remove duplicate data in multiple field combinations, and a keyword must be added before the first field. All other syntax is wrong.
SELECT DISTINCT rule FROM appuser;
4. Conditional query WHERE conditional splicing AND logic and OR logic or
#查询部门ID为50或者49,且状态为使用的状态
SELECT *
FROM appuser
WHERE (deptid=50 OR deptid=49) AND state=1;
1. Control conversion IFNULL (expr1, expr2)
IFNULL(expr1,expr2) #Convert the null value of field expr1 to expr2
2. The difference in days between two time periods DATEDIFF (expr1,expr2)
DATEDIFF(expr1,expr2) #time difference expr1-expr2, the number of days difference
3. Get the current time NOW()
NOW() #Get the current time
4. Arithmetic operators
expression |
significance |
example |
+ |
addition |
1+2+3 |
|
subtraction |
1-2-3 |
* |
multiplication |
1*2*3 |
/ |
division |
30/5 |
% |
modulus |
10%3 |
5. Comparison operators
6. Regular expression: REGEXP
Regular expression: REGEXP ^[\\u4e00-\\u9fa5]{2,4}$ Chinese character set, 2 characters to 4 characters
7. Logical operators
XOR two results one is true, one is not true, the result is true
8. Bitwise operators
9. Execution order of query conditional statements
3. Advanced query of database
1. Aggregation function
1. AVG() average value
AVG() average counts numbers don't count strings
2. SUM() Summing
SUM() Summing
3. MAX() Find the maximum value
MAX() Find the maximum value
4. MIN() Find the minimum value
MIN() finds the minimum value, and the date can also find the maximum and minimum values
5. COUNT(*) counts the number of entries
COUNT(*) counts the number of entries
Or COUNT (column name) to count the number of non-null data
6. Precautions for aggregate functions
Aggregate functions cannot appear in the WHERE clause, because they cannot be calculated without locking the data source. Contradictory.