create table `dept`(
`dept1` VARCHAR(10),
`dept_name` VARCHAR(20)
)
create table `emp`(
`id` varchar(10),
`name` VARCHAR(25),
`age` VARCHAR(10),
`worktime` VARCHAR(10),
`dept2` varchar(10),
`incoming` int(10)
)
insert into emp (id,name,age,worktime,incoming,dept2) VALUES (1500,'wangyi',25,'2022/2/1',8900,101),(1501,'hufei',27,'2022/3/1',7500,102),(1502,'heisen',30,'2022/3/20',8900,102),(1503,'huahudie',25,'2022/8/9',9000,102),(1504,'baimu',25,'2022/01/01',4000,103),(1505,'leilei',27,'2016/05/09',27800,106),(1506,'liuxiaoyi',28,'2017/05/09',16000,105),(1507,'haiwu',27,'2018/09/06',6300,104)
SELECT name,dept_name from dept left JOIN emp on dept.dept1 = emp.dept2
SELECT sum(incoming),dept_name from emp RIGHT JOIN dept ON emp.dept2 = dept.dept1 where dept_name=‘yunwei’
SELECT dept_name,id from emp RIGHT JOIN dept on emp.dept2 = dept.dept1 WHERE dept_name = ‘HR’
select name from dept LEFT JOIN emp on emp.dept2=dept.dept1 where dept_name=‘chaiwu’ and incoming>5000
select min(incoming),emp.worktime from dept RIGHT JOIN emp on dept.dept1 = emp.dept2 where dept_name=‘xiaoshou’ GROUP BY worktime
select name,id,dept_name from dept left JOIN emp on dept.dept1 = emp.dept2 WHERE age<(SELECT avg(age) from emp)
SELECT dept_name from emp LEFT JOIN dept on emp.dept2 GROUP BY dept_name HAVING sum(incoming)>10000
SELECT name from emp LEFT JOIN dept on emp.dept2 = dept.dept1 WHERE incoming < ‘10000’ && dept.dept_name=‘chaiwu’
SELECT name,min(incoming) from emp LEFT JOIN dept on emp.dept2 = dept.dept1 where dept_name=‘chaiwu’ GROUP BY name
SELECT emp.name from emp LEFT JOIN dept on emp.dept2 = dept.dept1 where age=(SELECT max(age) from emp LEFT JOIN dept on emp.dept2 = dept.dept1 WHERE dept.dept_name =‘yunwei’) and dept.dept_name=‘yunwei’
SELECT emp.name,dept.dept_name from emp LEFT JOIN dept on emp.dept2 = dept.dept1 where incoming=(SELECT min(incoming)from emp join dept on emp.dept2 = dept.dept1)
SELECT emp.incoming,dept.dept_name from emp LEFT JOIN dept on emp.dept2 = dept.dept1 where emp.name
=‘leilei’
SELECT emp.id,dept.dept_name,name
from emp LEFT JOIN dept on emp.dept2 = dept.dept1 where emp.incoming<8000
SELECT any_value(name),max(incoming),any_value(dept_name) from dept LEFT JOIN emp on dept.dept1 = emp.dept2 GROUP BY dept_name
由于5.7mysql以上版本不对不需要group by进行去掉会报错,所以需要在不需要group by的名字前添加any_value(),()里面时你的字段
select * from (select name,id,incoming from dept left join emp on dept.dept1=emp.dept2 where dept_name=‘yunwei’ order by incoming desc)as n where id limit 0,2;
SELECT id,name from emp LEFT JOIN dept on emp.dept2 = dept.dept1 WHERE dept_name=‘yunwei’ and incoming<(
SELECT avg(incoming) from emp LEFT JOIN dept on dept.dept1 = emp.dept2)
SELECT DISTINCT(dept_name) from dept LEFT JOIN emp on dept.dept1=emp.dept2 where dept1 in(SELECT dept2 from dept LEFT JOIN emp on dept.dept1 = emp.dept2 GROUP BY dept2 HAVING(COUNT(dept2)>1))
SELECT age,id from dept LEFT JOIN emp on dept.dept1 = emp.dept2 where incoming<=7500 and incoming>=3000
SELECT DISTINCT(dept_name) from emp LEFT JOIN dept on emp.dept2 = dept.dept1 WHERE worktime=2022
SELECT dept_name from emp LEFT JOIN dept on emp.dept2 = dept.dept1 WHERE name
=‘haiwu’
SELECT name,dept_name from dept LEFT JOIN emp on dept.dept1 = emp.dept2 where age in(SELECT min(age) from dept LEFT JOIN emp on dept.dept1 = emp.dept2 GROUP BY dept_name)
SELECT sum(incoming),any_value(dept_name) from dept LEFT JOIN emp ON dept.dept1 = emp.dept2 GROUP BY dept1
SELECT id,dept_name from dept LEFT JOIN emp on dept.dept1=emp.dept2 where incoming >6000
SELECT any_value(dept_name) from dept LEFT JOIN emp on dept.dept1=emp.dept2 GROUP BY dept1 HAVING (COUNT(id)=0)
SELECT * FROM dept LEFT JOIN emp on dept.dept1=emp.dept2 ORDER BY dept1 desc,worktime DESC
SELECT id,name,age from dept LEFT JOIN emp on dept.dept1=emp.dept2 where dept_name=‘yunwei’ and incoming>=7500
SELECT name,worktime from emp LEFT JOIN dept on emp.dept2 = dept.dept1 WHERE worktime>=2022 ORDER BY worktime
SELECT name,age,incoming from emp LEFT JOIN dept on emp.dept2 = dept.dept1 where worktime>=2022/01/01
SELECT name,incoming FROM emp where incoming>4000
SELECT sum(incoming),any_value(dept_name) from emp LEFT JOIN dept on emp.dept2 = dept.dept1 GROUP BY dept_name ORDER BY any_value(incoming) desc