MySQL usage example

1. Create a dept table:
create table dept--dept table
(deptno int primary key,--department ID
dname varchar(30),--department name
loc varchar(30));--dept address
where deptno is the primary key.
Insert data into this table:
insert into dept values(30,'Art Department','Nanhai');

2. Create an emp table:
create table emp--employee table
(empno int primary key,--employee number
ename varchar(30 ),--employee name
job varchar(30),--job name
mgr int,--superior employee number
hiredate datetime,--entry time
sal numeric(8,2),--salary
comm numeric(8,2), --bonus
deptno int,--department number
foreign key(deptno) references dept(deptno));--foreign key
where deptno is a foreign key, pointing to the deptno of the dept table.

3. Example application
3.1. Simple query (single table query)
-- view the attribute field of the table desc emp;
-- view the record select * from epm; (best not to use *)

--Query and eliminate duplicate rows select distinct deptno from emp; --Calculate
annual salary (including bonus) select ename,(if (comm is null,sal,sal+comm))*13 'Annual salary' from emp; (where , sql server writes select ename like this, (sal+isnull(comm,0))*13 'annual salary' from emp;)
--find employees who joined after 1982.1.1 select * from emp where hiredate > '1982-1- 1';
--Display salary from 2000 to 2500 select * from emp where sal between 2000 and 2500; (including both 2000 and 2500)
--Fuzzy query, query the name and salary of employees whose first letter is s select ename,sal from emp where ename like 'S%'; (case insensitive)
--Use the in keyword to implement batch queries select * from emp where empno in(123,345,800); --Display
employees without superiors select * from emp where mgr is null;
--Query employees whose salary is higher than 500 or whose position is manager, and satisfy their initials J select * from emp where (sal > 500 or job = 'manager') and ename like 'j%';
--Display employee information from low to high salary (default asc (ascending), desc (desc)) select * from emp order by sal;
--Sort by department number in ascending order and employee's salary in descending order select * from emp order by deptno, sal desc; --display
average salary and salary sum select avg(sal) average salary, sum(sal) total salary from emp;
-- Count how many employees there are select count(*) from emp; --display
the lowest salary, and display the name of the employee select ename,sal from emp where sal = (select min(sal) from emp);(more troublesome)
--display Employees whose salary is higher than the average salary select * from emp where sal > (select avg(sal) from emp);
-- display the average salary and maximum salary of each department select avg(sal),deptno,max(sal) from emp group by deptno; --Display
the average salary and maximum salary of each position (same position between different departments) select avg(sal),min(sal),deptno,job from emp group by deptno,job order by deptno;
- - Display the department with average salary below 2000 and its average salary select avg(sal), deptno from emp group by deptno having avg(sal) < 2000;

3.2, complex query (multi-table query)
- display employee name, salary and the name of the department select ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
--Display the sales department location and its employee name select ename,dname,loc from emp,dept where dname='sales' and emp.deptno=dept.deptno; --Display
the department name, employee name and salary select with department number 10 d.dname,ename,sal from emp e,dept d where e.deptno=10 and e.deptno=d.deptno; (the alias must be used after setting the alias)
-- display the employee's name, salary, and the name of the department , and sort by department select ename,sal,dname from emp,dept where emp.deptno=dept.deptno order by emp.deptno;

3.3. Complex query (self-connection query)
-- Display the name of an employee's superior (such as ford) select ename from emp where empno=(select mgr from emp where ename="ford");
--Display the name of each employee and the name of his superior select worker.ename workername,boss.ename bossname from emp worker, emp boss where worker.mgr=boss.empno; (Note: this statement will not display when the employee does not have a superior. If the employee does not have a superior, an outer connection needs to be used, which will be mentioned later) 3.4 .

Subquery--
Show all employees in the same department as smith select * from emp where deptno=(select deptno from emp where ename="smith");
--Query the name, position, salary and department number of the same employee working in department No. 10 select ename,job,sal,deptno from emp where job in(select distinct job from emp where deptno=10); --display
higher than Employee information of department average salary select ename,sal,tem.myavg,emp.deptno from emp,(select avg(sal) myavg,deptno from emp group by deptno) tem where emp.deptno=tem.deptno and emp.sal> tem.myavg;

3.5, paging query
--display the first to fourth employees select * from emp order by hiredate limit 0,4; (the first parameter 0 represents the first record, the second parameter 4 means to take four records, from which the first to fourth records can be taken out. In addition, the syntax is mysql database, and the syntax in sql server is select top 4 * from emp order by hiredate, there is a big difference between the two)
- -Display the fifth to tenth employees select * from emp order by hiredate limit 4,6; (this statement applies to mysql, the statement in sql server is select top 6 * from emp where empno not in(select top 4 empno from emp order by hiredate) order by hiredate)

3.6, outer join
--Display the name of each employee and the name of his superior, if there is no superior, it will display null select worker.ename workername,boss.ename bossname from emp worker left join emp boss on worker.mgr=boss.empno;
  say here Look at the left outer join and right outer join, left outer join: the records of the table on the left are all displayed, and if there is no matching record, it is filled with null, and the right outer join is just the opposite.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522193&siteId=291194637