当前位置:网站首页>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
边栏推荐
- Get bat command results in bat
- 软件项目管理 8.4.软件项目质量计划
- UnityShader入门精要——Unity中的渲染优化技术(四)
- MCS:离散随机变量——Bernoulli分布
- Complete the primary school project in 3 days, and teach you to complete the weather broadcast system hand in hand!
- 关于重放攻击和防御
- MCS:离散随机变量——Uniform分布
- Arduino temperature and humidity sensor DHT11 (including code)
- BGP experiment
- What do Niu B programmers pay attention to when "creating an index"?
猜你喜欢

MCS: continuous random variable lognormal distribution

Visual display of TEQC quality analysis results using teqcplot

Open source ecology 𞓜 super practical open source license basic knowledge literacy post (Part 2)

JDBC入门学习(三)之事务回滚功能的实现

Web 应用程序安全测试指南

应用挂了~

MCS:连续随机变量——Student’s t分布

Memory model of JVM principle

Hcip switch experiment

JVM原理之完整的一次GC流程
随机推荐
【微服务|Nacos】Nacos版本相关问题一览
Drag and drop拖放框架
In unity, how to read and write a scriptableobject object in editor and runtime
C language stack implementation
Mysql入门学习(三)之视图
markdown给图片加背景色
Introduction and use of precise ephemeris
JDBC入门学习(一)之DML操作
BGP second test
How can functional testers spend one month to become advanced automation software test engineers
Array The from method creates an undefined array of length n
Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
JVM原理之内存模型
Web 应用程序安全测试指南
App hangs~
Error related to synchronizing domestic AOSP code
[leetcode] longest increasing subsequence problem and its application
Zygote process
PHP move_ uploaded_ File failed to upload mobile pictures
小时候 觉得爸爸就是天 无所不能~