This article brings you relevant knowledge about mysql, which mainly introduces the relevant content about multi-table query and some case sharing, including querying the name, age, position of employees, etc. Let’s take a look at the content below, I hope it will be helpful to everyone.
Recommended learning: mysql video tutorial
create table salgrade( grade int, losal int, hisal int ) comment '薪资等级表'; insert into salgrade values (1,0,3000); insert into salgrade values (2,3001,5000); insert into salgrade values (3,5001,8000); insert into salgrade values (4,8001,10000); insert into salgrade values (5,10001,15000); insert into salgrade values (6,15001,20000); insert into salgrade values (7,20001,25000); insert into salgrade values (8,25001,30000);
In this case, we mainly use the multi-table query syntax explained above to complete the following 12 requirements. There are only three tables involved here: emp employee table , dept department table, salgrade salary grade table.
Table: emp, dept
Join condition: emp .dept_id = dept.id
select e.name , e.age , e.job , d.name from emp e , dept d where e.dept_id = d.id;
Table: emp, dept
Connection conditions: emp.dept_id = dept.id
select e.name , e.age , e.job , d.name from emp e inner join dept d on e.dept_id =d.id where e.age < 30;
Table: emp, dept
Connection conditions: emp.dept_id = dept.id
select distinct d.id , d.name from emp e , dept d where e.dept_id = d.id;
Table: emp, dept
Connection conditions: emp.dept_id = dept.id
select e.*, d.name from emp e left join dept d on e.dept_id = d.id where e.age >40 ;
-- 方式一 select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary >= s.losal and e.salary <= s.hisal; -- 方式二 select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary between s.losal and s.hisal;Recommended learning:
The above is the detailed content of MySQL basic multi-table query case sharing. For more information, please follow other related articles on the PHP Chinese website!