当前位置:网站首页>Introduction to MySQL (I) grammar
Introduction to MySQL (I) grammar
2022-06-23 05:23:00 【The silent Lord returns to the sand】
1.MySQL Introduce
Data storage problem :
Variable , Array , object , aggregate
disadvantages : Temporary storage , The magnitude of stored data is too small
IO flow
disadvantages : The stored data is not type specific , No security measures , No backup and recovery
Introduce the concept of database :
Store and manage data warehouse
Database classification :
1. Relational database :
Oracle、DB2、MySQL( a key )、SQL Server, characteristic : Tables are associated with tables
2. Non relational database :
ElastecSearch、MongoDB、Redis, Hash tables are commonly used , Save data through key value pairs
Database installation ---- Text interface installation mode
SQL Language Overview
Structured query language , Simply put, adding, deleting, modifying and checking are all SQL Category –CRUD
1. Operate the database from the command prompt
- Operate the database graphically



- Data query ( a key )
The basic query
ctrl+: Zoom in
The query syntax :SELECT Name FROM Table name Query a single field
select first_name from t_employees;
Query multiple fields
select first_name,salary from t_employees;
Query all fields
select * from t_employees;
The query field can be operated
SELECT employee_id,first_name,salary*12 from t_employees;
Set the alias of the query field : as
SELECT employee_id as ' Employee number ',first_name as ' surname ',salary*12 as ' Annual salary ' from t_employees;
De duplication of field contents :distinct
select DISTINCT manager_id from t_employees;
Sort query : SELECT Name FROM Table name ORDER BY Sort columns [ Sort rule ]
Check the employee's number , name , Salary . Sort by salary in descending order .
select employee_id,first_name,salary from t_employees ORDER BY salary desc;
Multi column sorting :
Check the employee's number , name , Salary . Sort by salary in ascending order ( When the salary is the same , Sort by number in ascending order ).
select employee_id,first_name,salary from t_employees ORDER BY salary asc,employee_id asc;
Conditions of the query
Conditions of the query :
grammar :SELECT Name FROM Table name WHERE Conditions
Equivalent judgment (=)
Query salary is 11000 Employee information ( Number 、 name 、 Salary )
select employee_id,first_name,salary from t_employees where salary=11000;
Logical conditions (and,or,not)
Query salary is 11000 And the Commission is 0.30 Employee information ( Number 、 name 、 Salary )
select employee_id,first_name,salary from t_employees where salary=11000 and commission_pct=0.3;
Unequal value judgment (> 、< 、>= 、<= 、!= 、<>)
Query employee's salary in 6000~10000 Employee information between ( Number , name , Salary )
select employee_id,first_name,salary from t_employees where salary>=6000 and salary<=10000;
Interval judgment (between and)
Query employee's salary in 6000~10000 Employee information between ( Number , name , Salary )
select employee_id,first_name,salary from t_employees where salary BETWEEN 6000 and 10000;
null Value judgement
Search for employee information without commission ( Number , name , Salary , Royalty )
select employee_id,first_name,salary,commission_pct from t_employees where commission_pct is NULL;
where in Enumerate queries
Inquiry Department No 70、80、90 Employee information ( Number , name , Salary , Department number )
select employee_id,first_name,salary,department_id from t_employees where department_id in (70,80,90);
Fuzzy query : like % _
Look up the name with "L" Employee information at the beginning ( Number , name , Salary , Department number )
select employee_id,first_name,salary,department_id from t_employees where first_name like 'L%'
Look up the name with "L" Starts with a length of 4 Employee information ( Number , name , Salary , Department number )
select employee_id,first_name,salary,department_id from t_employees where first_name like 'L___';
Branch structure query
Query employee information number , name , Salary , Salary level The corresponding conditional expression is generated
select employee_id,first_name,salary,
(case
WHEN salary>=10000 THEN 'A'
WHEN salary>=8000 and salary<10000 THEN 'B'
when salary>=6000 and salary<8000 then 'C'
WHEN salary>=4000 and salary<6000 THEN 'D'
else 'E'
END) as ' Salary level '
from t_employees;
System function query
The query time : grammar :SELECT Time function
select SYSDATE(); Query the current system date and time
SELECT NOW(); Query the current date and time
SELECT CURDATE(); Query date
select CURTIME(); Query time
select YEAR(NOW()); Query year
SELECT DATEDIFF('2021-08-09','2021-08-01'); The difference between two time days
SELECT ADDDATE('2021-08-09',10); At a given time , Plus days
String query :
select CONCAT(CURDATE(),' ',CURTIME()); String splicing
SELECT INSERT('helloworld',2,3,"xx"); hxxoworld Add a substring to the specified position of the original string
The subscript of the database often starts from 1 Start
polymerization & grouping & Filter & Restrict queries
Aggregate functions
grammar :SELECT Aggregate functions ( Name ) FROM Table name ;
Count the total monthly salary of all employees
select sum(salary) from t_employees;
Find the total number –COUNT( Field ): barring null Number of values
SELECT count(commission_pct) from t_employees;
Find the total number , contain null The record of value ( Commonly used )
select count(*) from t_employees;
Group query :
grammar :SELECT Name FROM Table name WHERE Conditions GROUP BY Group by ( Column )
Case study : Query the total number of people in each department
Ideas :
1. Group by department number ( The grouping is based on department_id)
2. Then make statistics on the number of people in each department (count)
select department_id,count(*) from t_employees GROUP BY department_id;
Case study : Query each department 、 Number of people in each position
Ideas :
1. Group by department number ( Group by department_id).
2. Group by position name ( Group by job_id).
3. Count the number of people for each position in each department (count).
select department_id,job_id,count(*) from t_employees GROUP BY department_id,job_id;
Query each department id、 The total number of 、first_name ---- Wrong query
Be careful : The field matching the grouping query must be , Aggregate function or group by column
select department_id,count(*),first_name from t_employees GROUP BY department_id;
Group filter query
grammar :SELECT Name FROM Table name WHERE Conditions GROUP BY Group columns HAVING Filtering rules
Case study : Statistics 60、70、90 The highest salary in department No
Ideas :
1). Determine the grouping basis (department_id)
2). For grouped data , The filtered department number is 60、70、90 Information
3). max() Function processing
select department_id,max(salary) from t_employees GROUP BY department_id HAVING department_id in (60,70,90);
Limited query - Limit the number of queries
SELECT Name FROM Table name [LIMIT Start line , Number of query lines ]
Case study : Query all the information of the top five employees in the table
select * from t_employees limit 0,5; front 5 strip Subscript from 0 Start
select * from t_employees limit 5,5; the second 5 strip
边栏推荐
- MCS: continuous random variable - student's t distribution
- Calculate Euclidean distance and cosine similarity
- markdown给图片加背景色
- 大环境不好难找工作?三面阿里,幸好做足了准备,已拿offer
- 【微服务|Nacos】Nacos版本相关问题一览
- Introduction to s file generated by TEQC for GNSS data quality analysis
- Strong push, quick start to software testing
- Open source ecology 𞓜 super practical open source license basic knowledge literacy post (Part 2)
- Visual display of TEQC quality analysis results using teqcplot
- Konva series tutorial 1:what is konva?
猜你喜欢

App hangs~

99 multiplication table bat

What do Niu B programmers pay attention to when "creating an index"?

insert into... Where not exists insert to avoid repeated use

I have been engaged in software testing for 5 years and have changed jobs for 3 times. I have understood the field of software testing

What is the average annual salary of an outsourced tester who has worked for 5-8 years?

Introduction to unityshader -- rendering optimization technology in unity (IV)
![[microservices | Nacos] Nacos realizes data isolation of multi environment and multi tenant](/img/dc/f00a780e96032c0b7d9b6d7c2267d1.png)
[microservices | Nacos] Nacos realizes data isolation of multi environment and multi tenant

JVM原理之内存模型

组合式API-composition-api
随机推荐
【opencv450】帧间差分法
[laravel series 7.8] broadcasting system
I have been engaged in software testing for 5 years and have changed jobs for 3 times. I have understood the field of software testing
Chapter IX app project test (1)
pygame音乐相关的功能实现
Mysql入门学习(二)之子查询+关联
关于DOS/DDOS攻击和防御
渗透测试基础 | 附带测试点、测试场景
BGP experiment
【微服务|Nacos】Nacos版本相关问题一览
物联网开源开发平台 Shifu 开放内测!第一版技术文档发布
Jetpack Compose 从开门到入门之 MenuBar桌面菜单(Desktop Menu)
Rtklib new version 2.4.3 B34 test comparison
Event log keyword: eventlogtags logtags
JDBC入门学习(三)之事务回滚功能的实现
Hcip switch experiment
弱者易怒如虎,强者平静如水,真正厉害的人早已戒掉了情绪
H5 适配全面屏
Fund performance evaluation
A bug in rtklib2.4.3 B34 single point positioning