Summary of mysql basic knowledge points (1)

This article is the notes recorded by me during my study. It is convenient for repeated review later. There may be errors. Please understand. It is a good habit to like the article before reading it.

Log in to mysql locally:
mysql -uroot -p123456
exit mysql
exit
1. Single table query
1.
Check which databases mysql has
show databases;  choose to use the database use test; // Create a database
using a database called test : create database fujianjian;/
/Create a database called fujianjian.
Display all tables under the database: show tables;  
2.DQL
select dname from dept;
3.
select dname,deptno from dept;
4.
select * from dept;//Not recommended, performance will be very low
5.
select deptno,dname as deptname from dept;
6.
select deptno,dname deptname from dept;
7.
select deptno,dname 'dept name'from dept;
(emphasize that single quotes are the most standard in databases)
8.
select ename,sal*12 from emp ;
select ename sal*12 as yearsal from emp;
---条件查询?
9.
select ename, empno from emp where sal=800;
select ename, empno from emp where sal!=800;
select ename ,empno from emp where sal<>800;(也是不等于)
10.
select ename,empno,sal from emp where sal<2000;
select ename,empno,sal from emp where sal<=2000;
select ename,empno,sal from emp where sal>=2000;
11.
select empno,enaem,sal from emp where sal >=2450 and sal<=3000;
select empno,enaem,sal from emp where sal between 2450 and 3000;
12.
select empno,ename,sal,comm from emp where comm is null;
select empno,ename,sal,comm from emp where comm is not null;
13.
select empno,ename,job,sal from emp where job='lisi' and sal>2500;
14.
select empno,ename,ob from emp where job='haha' or job='niuniu';
15.
select empno,ename,ob from emp where sal>2500 and (deptno=10 or detno=20);
16.
select empno,ename,ob from emp where job='haha' or job='niuniu';
select empno,ename,ob from emp where job in ('haha','niuniu');
select empno,ename,ob from emp where job not in ('haha','niuniu');//括号里面只代表具体值,不代表区间
17.select ename from emp where ename like 'K%'; (starts with K)select ename from emp where ename like '%T'; (with T end)select ename from emp where ename like '%O%'; (with O in the middle) 
like fuzzy query % (any number of characters)_ (any one character)



select ename from emp where ename like '_A%'; (the second letter is A)
select ename from emp where ename like '__R%'; (the third letter is R)
select ename from emp where ename like ' %\_%';(\escape, find the ones containing underscores)
18.
select ename,sal from emp order by sal;(The default is ascending order)
select ename,sal from emp order by sal desc;(desc descending order)
select ename,sal from emp order by sal asc; (asc ascending order)
Query the employee name and salary in ascending order of salary. If the salary is the same, sort by name in ascending order;
select ename,sal from emp order by sal asc,ename asc;
//sal is in the front, taking the leading position. Only if the front is the same, the subsequent sorting will be executed
select ename, sal from emp order by 2; //2 represents the second column. The second column is sal;
                        //It is not recommended to write like this in development because it is not robust;
19.
select ename,sal from emp where sal between 1250 and 3000 order by sal desc; (the order cannot be changed)
 20. Single row data processing function :
20.1.
select lower(ename) as ename from emp;//lower converts to lowercase
select upper(ename) as ename from emp;//upper converts to uppercase
20.2
select substr(ename,1,1) as ename from emp;(start The position starts from 1) //substr takes the substring (substr (intercepted string, starting subscript, intercepted length))
20.3 
select length (ename) enamelength from emp; //length takes the length  
select concat(empon, ename) from emp; //concat function performs string splicing
20.4 
select * from emp where ename=trim(' KING'); //trim() removes spaces
20.5 
select 'abc' from emp; generates 14 abc //select Followed by a literal value, the quantity value of the table structure will be automatically generated
       select round(123.567,0) as result from emp;(0 retains the integer part, 1 retains one decimal place. -1 retains the tens and ones digits Fill with 0) //round generates a random number
20.6 
 select round(rand()*100,0) from emp; //Generate //rand() generates a random number
20.7  from 0 to 1
The results of mathematical operations involving null values ​​are all null, and addition cannot
select ename, (sal+ifnull (comm, 0)) * 12 as yearsal from emp; //ifnull null value processing function ifnull (data, is regarded as that value)
20.8 
case...when...then...when...then...else...end;
when the employee's job is manager, the salary is increased by 10%, when... ;
select ename,job,sal from emp;
select ename,job, (case job when 'manager' then sal*1.1 when 'salesman' then sal*1.5 else sal end) as newsal from emp; 21. Grouping function, multiple
lines Processing function, input multiple lines, and finally output one line.
21.1 
five count count sum sum avg average max maximum value min minimum value 
select sum (salary) from emp; //Aggregation function can be used only after grouping first. After select, the entire table is defaulted into one group. If there is no grouping after where, Error report
//count will automatically ignore null, and the grouping function will ignore null. These five functions will all ignore null.
21.2
count The difference between a specific field and count(*); count(*) calculates the number of rows in the entire table; count A field will automatically ignore null;
21.3
The grouping function cannot be used directly in the where clause because the execution order is (from>where>group by >select>order by) because there is no grouping at the time of where
22. Group query (very important; five stars*****) select ... from... where..group by...order by... The execution sequence is explained above.
22.1
Find the salary of each position and//Group according to the job position, and then sum the salary
select job, sum ( sal) from emp group by job; note that only grouping keywords and grouping functions can be added to the query field, and others cannot be followed;
select deptno, max (sal) from emp group by deptno;
22.2
Find out the numbers of each department and different jobs Maximum salary; //Technique: Combine the two fields into one field to view. (Joint grouping of two fields)
select job, deptno, max (sal) from emp group by deptno, job;
22.3 
Use having to filter. It must be used in conjunction with group by to
find the highest salary of each department. It is required to display the highest salary greater than 3000?
select deptno, max(sal) from emp group by deptno having max(sal)>3000;
//For data that can be filtered out by where, use where to filter it first. The efficiency will be higher than having. If where cannot solve the problem, use having. sentence
select deptno, max (sal) from emp where sal>3000 group by deptno;
where there is no way? Find out the average salary of each department and request to display the salary higher than 2500;
select deptno, avg (sal) from emp group by deptno having avg (sal) > 2500;
A summary of the single table query:
select ... from. ..where...group by...order by...
Execution order 1.from 2.where 3.group by 4.having 5.select 6.order by
to find the average salary of each position and require the average to be displayed If the salary is greater than 1500, except
for manager positions, it is required to select job in descending order of average salary , avg (sal) as avgsal from emp where job <> 'manger' group by job having avg(sal)>1500 order by avgsal desc ;
------------------------------------------------ -------------------------------------------------- ----------------------------------

Guess you like

Origin blog.csdn.net/m0_55315930/article/details/121620332