[Edit, test and edit] MySQL database basic knowledge 2

1. Common functions:
character function: length concat substr instr trim upper lower lpad rpad replace
Mathematical function: round ceil floor truncate mod
Date function: now curdate curtime year month monthname day hour minute second str_to_date date_format
1. Character function
#length Get parameter value the number of bytes
the SELECT the LENGTH ( 'John'); #. 4
the SELECT the LENGTH ( 'Chi Master hahaha'); # 15, utf8 a character, 3 bytes
#concat string concatenation (underlined splice)
the SELECT CONCAT (Last name ,' ',first_name) FROM manba;
#upper,lower SELECT UPPER('john');#变Uppercase SELECT LOWER('JOHN');#变小case
#Example: Change the last name to uppercase, the first name to lowercase, splicing
SELECT CONCAT (UPPER(last_name),LOWER(first_name)) AS name FROM manba;
#substr,substring #Note
that the index starts from 1, and the next statement output: and李四#Intercept
all characters after the specified index
SELECT SUBSTR('张三和Li Si', 3) out_put;
#Cutting from the specified character length at the specified index
#The following statement output: Zhang San
SELECT SUBSTR('张三和李四',1,2) out_put; #Case
: The first character in the name is uppercase, the other characters are lowercase, use _ Splicing, display
SELECT CONCAT(UPPER(SUBSTR(last name,1,1)),' ',LOWER(SUBSTR(last_name,2))) output FROM manba;
#instr #Return
to the starting index of the substring, can’t be found return to 0
SELECT INSTR ( 'love with Yin Yang Buhui six Man', 'six Yin Xia') the AS out_put
#trim removed and trailing
# output Cuishan
SELECT LENGTH (TRIM ( 'Cuishan')) out_put the AS;
# aaaa Greenfield of outputs
SELECT TRIM('a' FROM'aaaa张aaaa Cuishanaaaaaaaa') AS out_put;
#lpad Use the specified characters to realize the left padding the specified length
#output* ** Yin Susu
SELECT LPAD(' Yin Susu

',10,' ') AS out_put; #Output : Yin Su SELECT LPAD('Yin Susu ',2,' ') AS out_put;
#rpad Use the specified characters to realize the right padding and the specified length
#Output: Yin Susu abababa
SELECT RPAD('Yin Susu',12,'ab') AS out_put;
#replace Replace
SELECT REPLACE('Zhang Wuji fell in love with Zhou Zhiruo','Zhou Zhiruo','Zhao Min') AS out_put;

2. Number function
#round
SELECT ROUND(1.65); #2
SELECT ROUND(-1.45); #-1
SELECT ROUND(1.567, 2); #1.57, keep 2 digits after the decimal point
#ceil round up (return>= The smallest integer of the parameter)
SELECT CEIL(-1.02);#-1
SELECT CEIL(1.00);#1
#floor Round down, return <= the largest integer of the parameter
SELECT FLOOR(-9.99);#-10
# truncate
SELECT TRUNCATE(1.65,1);#1.6;
#mod remainder
mod(a,b): aa/b b
mod(-10,-3): -10-(-10)/(-3)
( -3)=-1;
SELECT MOD(10,-3);#1

3. Date function
#now: returns the current system date plus time
SELECT NOW();
#curdate returns the current system date, excluding the time
SELECT CURDATE();
#curtime() returns the current time, excluding the date
SELECT CURTIME();
# Can get the specified part, year, month, day, hour, minute, second
SELECT YEAR(NOW()) AS year;
SELECT YEAR('1998-1-1') year;
SELECT YEAR(hiredate) year FROM employees;
SELECT MONTH(NOW()) month;
SELECT MONTHNAME(NOW()) month;
#display English month# str_to_date converts the characters in the date format to the date in the specified format
%Y four-digit year
%y two-digit year
%m month( 01,02,...12)
%c month (1,2,..., 12)
%d day
%H hour (24) %h (12)
%i minute %s second
SELECT STR_TO_DATE('9-13 -1999','%m-%d-%Y') date;#1999-09-13
SELECT STR_TO_DATE ('2020-4-17','%Y-%c-%d') AS output;#2020 -4-17
#Query the information of employees whose entry date is 1992-4-3
SELECT FROM employees WHERE hiredate='1992-4-3';
SELECT
FROM employees WHERE hiredate=STR_TO_DATE('4-3 1992','%c-%d %Y');
#date_format Convert dates into characters
SELECT DATE_FORMAT( NOW(),'%yyear%mmonth%dday') AS output;#20年4月17日
#Query the employee name and entry date with bonus (xx month/xx day xx year)
SELECT last_name,DATE_FORMAT(hiredate ,'%m month/%d day%Y year') entry date
FROM employees
WHERE commission_pct IS NOT NULL;

4. Aggregate functions
# simply use
SELECT SUM(salary) FROM manba;
SELECT SUM(salary) and ROUND(AVG(salary), 2) average FROM manba;
SELECT MAX(salary) is the highest, MIN(salary) is the lowest FROM manba ;
the SELECT MIN (salary) the FROM Manba;
the SELECT COUNT (salary) the FROM Manba;
# parameter supports what type
the SELECT SUM (last_name), AVG (last_name) the FROM Manba;
the SELECT SUM (the HireDate), AVG (the HireDate) the FROM Manba;
# no significance is not so with
the SELECT MAX (last_name), MIN (last_name) the FROM Manba;
the SELECT MAX (HireDate), MIN (HireDate) the FROM Manba;
# support
SELECT COUNT (last_name) FROM manba; # calculating a non-null value 107 SELECT COUNT (commission_pct) FROM manba;#35 #Whether to
ignore null
SELECT SUM(commission_pct), AVG(commission_pct) FROM manba; #With
distinct
SELECT SUM(DISTINCT salary), SUM(salary) FROM manba;
SELECT COUNT(DISTINCT salary), COUNT(salary) FROM manba;
#count function details
SELECT COUNT(salary) FROM manba;
SELECT COUNT(*) FROM manba;
#Count the number of each column, that is, the number of all rows SELECT COUNT(1) FROM manba;#The effect of the previous statement is the same.
#6. There are restrictions on the fields queried together with the grouping function
SELECT AVG(salary),manba _id FROM manba;# This employee id is meaningless

2. Group query
syntax select group function (max, min, etc.), column (required to appear after group by) fro table
[where filter condition] group by grouped list [order by] clause
Note: the query list must be special and required It is the field that appears after the grouping function and group by
# Simple grouping query
# Case 1: Query the highest salary of each job
SELECT MAX (salary), job_id
FROM manba
GROUP BY job_id;
# Case 2: Query the department at each location Number of
SELECT COUNT( ),location_id
FROM departments
GROUP BY location_id; #added
group pre-filtering conditions
#Case 1: Query the average salary of each department with a character in the mailbox
SELECT AVG(salary), department_id
FROM manba
WHERE email LIKE '%a%'
GROUP BY department_id; #Case
2: Query the highest salary of each leader's employee with bonus
SELECT MAX(salary),manager_id
FROM manba
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
#



加组后的滤时间#Case 1: Query which department has more than 2 employees # ①Query the number of employees in each department SELECT COUNT( ),department_id FROM manba
GROUP BY department_id;
# ②Filter according to the result of 1
SELECT COUNT( ),department_id
FROM manba
GROUP BY department_id
HAVING COUNT(
)>2; #Case
2: Query the maximum salary of employees with bonuses for each type of work>12000 work type number and its maximum salary
#①Query each type of work with bonus the staff of the highest wages
the SELECT MAX (salary), job_id
the FROM Manba
the WHERE commission_pct iS the NOT NULL
the GROUP BY job_id;
# ② according to the results of a continuation of the screening, the highest salary> 12000
the SELECT MAX (salary), job_id
the FROM Manba
the WHERE commission_pct iS the NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;
#Case 3: Query the minimum wage of each leader whose leader ID>102>5000 leader ID
# ①Query the minimum wage of each leader whose leader number>102
SELECT MIN(salary), manager_id
FROM manba
WHERE manager_id>102
GROUP BY manager_id;
# ②On the basis of 1, the minimum wage>5000
SELECT MIN(salary), manager_id
FROM manba
WHERE manager_id>102
GROUP BY manager_id
HAVING MIN(salary)>5000 #group
by expression or function
#case: group by the length of the employee name, query the number of employees in each group, filter the number of employees>5
SELECT COUNT( ),LENGTH(last_name) len_name
FROM manba
GROUP BY len_name
HAVING COUNT(
)>5; #by
multiple field grouping
#Case: Query the average salary of employees of each type of work in each department
SELECT AVG(salary),department_id ,job_id
FROM manba
GROUP BY department_id,job_id; #added
sort
##Case: Query the average salary of employees of each type of work in each department, and sort the ones >10000 by high and low
SELECT AVG(salary) a,department_id,job_id
FROM manba
GROUP BY department_id,job_id
HAVING a>10000
ORDER BY AVG(salary) DESC;
Third, link query (also known as multi-table query, when the query field comes from multiple tables, just The connection query will be used)
syntax: select query list
from table 1 alias [connection type]
join table 2 alias on [connection condition]
on connection condition
[where filter condition]
[group by grouping]
[having filter condition]
[order by sort List]
Category: inner connection: inner
outer connection: left outer left [outer]
right outer right [outer]
full outer [outer]
cross connection: cross
1. inner connection (equivalent connection, non-equivalent connection, self-connection)
#等值连接#Case
1: Query the department name and the number of employees in which the number of employees in the department> 3, and in descending order
SELECT COUNT( ), department_name
FROM manba e
INNER JOIN departments d
ON e. department_id= d.department_id
GROUP BY department_name
HAVING COUNT(
)>3
ORDER BY COUNT( ) DESC; #Case
2: Query employee name, department name, job type name, and descending order by department name (three tables join)
SELECT last_name,department_name,job_title
FROM manba e
INNER JOIN departments d
ON e. department_id=d. department_id
INNER JOIN jobs j
ON e. job_id=j. job_id
ORDER BY department_name DESC;
#非等值#Query
the number of wages> 20 the number of levels, and in descending order of wages
SELECT COUNT(
) ,grade_level
FROM manbae
INNER JOIN job_grades g
ON e. salaryBETWEEN g. lowest_salAND g. highest_sal
GROUP BY grade_level
HAVING COUNT( )>20
ORDER BY grade_level DESC;
#自连接#Query
the name of the employee, the name of the superior
SELECT e.last_name,m.last_name
FROM manba e
JOIN manba m
ON e. manager_id=m manba_id.; #Plus
filter: the name of the employee whose name contains the character k, the name of the superior
SELECT e.last_name,m.last_name
FROM manba e
JOIN manba m
ON e. manager_id=m. manba _id
WHERE e. last_nameLIKE'%k%';
2. Outer join
#Query the girl name whose boyfriend is not in the boys table
#Left outer join
SELECT be.name,bo.

FROM beauty be
LEFT OUTER JOIN boys bo
ON be.boyfriend_id=bo.id
WHERE bo. idIS NULL;
#右外连接
SELECT be.name,bo.*
FROM boys bo
RIGHT OUTER JOIN beauty be
ON be.boyfriend_id=bo.id
WHERE bo. idIS NULL;

4. Subquery (select statement that appears in other statements is called subquery or inner query and
external query statement, called main query or outer query)
1. Query the name and salary of employees in the same department as Zlotkey
SELECT last_name, salary
FROM manba
WHERE department_id=(
SELECT department_id
FROM manba
WHERE last_name='Zlotkey');
#2, query the employee number, name, salary of the employee whose salary is higher than the company's average salary
SELECT last_name,employee_id, salary
FROM manba
WHERE salary>(SELECT AVG(salary)FROM manba);
#3. Query the number, name and salary of the
employees in each department whose salary is higher than the average salary of the department. SELECT employee_id,last_name,salary,e. department_id
FROM manba e INNER JOIN(SELECT AVG(salary) ag ,department_id
FROM manba
GROUP BY department_id)avg_dep
ON e. department_id=avg_dep.department_id
WHERE salary>avg_dep.ag;

#4. Query, and the employee number and name of the employee in the same department as the employee whose name contains the letter u
SELECT last_name,manba_id
FROM manba
WHERE department_id IN(
SELECT DISTINCT department_id
FROM manba
WHERE last_name LIKE'%u%');
# 5. Query the employee number of the
employee working in the department whose location id is 1700 SELECT employee_id
FROM manba
WHERE department_id=ANY(
SELECT department_id
FROM departments
WHERE location_id=1700);
#6. Query the name and salary of the employee whose manager is king
SELECT last_name, salary
FROM manba
WHERE manager_id IN(
SELECT employee_id
FROM manba
WHERE last_name='K_ing');
#7. Query the name of the employee with the highest salary, requiring first and last_name to be displayed as a column, the column name is
SELECT CONCAT(first_name, last_name) "Name"
FROM manba WHERE salary=(SELECT MAX(salary)
FROM manba

[Edit, test and edit] MySQL database basic knowledge 2

[Edit, test, edit] The teaching content is:

Basic knowledge of testing, project combat, test management, agile testing, exploratory testing, APP testing, Linux, database, test environment construction, Python programming, WEB UI automation testing, APP UI automation, interface function testing, performance testing, interfaces Automated testing, Jenkins continuous integration, etc.

Through learning, students can master the latest technology of software testing companies, benchmark the requirements of first-line Internet companies, so that students can reach the level of intermediate and senior test engineers, and can quickly integrate into the actual work of the company after graduation.

No matter you are a liberal arts student, a junior college student, a basic zero, or a female student, you can easily learn it!

Guess you like

Origin blog.51cto.com/14972695/2576667