在工作中很多时候,我们需要获取到具体的数据需要关联好几个表,才能够查询获得自己想要的数据,这样就需要几个表关联起来进行查询。
查询关联字段共同拥有的数据(重点 面试题)
首先创建三个表格
命令:select person.name,person.age,work.company,school.schoolName from person inner join work on person.code=work.code inner join school on work.code=school.code where person.code=1001;连接多个表格
将三个表格中共同拥有的数据code=1001,连接起来
select *from employees limit 5,
查询表里总数据:select count(*) from 表名;
select count(*) from employees;
select count(1) from employees;
and 并且的关系,多个条件要同时满足
select * from employees where first_name="Georgi"and last_name="Facello"and hire_date="1986-06-26";
select * from employees where first_name="Georgi"or last_name="admin" limit 10
select * from employees where last_name in("Christ","lamba","asdfg")limit 10;
select * from employees where hire_date between "1986-12-01"and "1986-12-31" -> limit 5;
前后都可以用%代替,进行模糊查找;
ka前面任意两个字符串,后面为其他字符串,几个下划线代表几个字符串
select * from employees where first_name like "__ka%"limit 5;
select * from employees where first_name rlike "^mu" limit 5;
select * from employees where first_name rlike "ro$" limit 5;
将count(1)改为总数
查询薪资最高的员工信息
select * from salaries order by salary desc limit 10;正序 由高到低排序
select * from salaries order by salary asc limit 10;倒序 由低到高排序(默认倒叙)
查询总数(count) 查询男女的薪水分别的总数
select salary,count(1) as count from salaries group by salary 【order by count desc】 limit 10;
备注解读:将薪水总数按正序排列
按照年份将员工薪资从高到低正序排列
{select year(from_date) as dateyear,sum(salary) as sumsalary from salaries} group by dateyear order by sumsalary desc limit 10;
备注:查看年份和薪水,将薪水总和按照正序排序
通过having来过滤group by字句的结果信息
找到平均薪资超过100000的员⼯
例:表e和表s通过emp_no内连接起来,查询e表中的first name、last name、gender和s表中的salary ,显示出salary大于40000的十行数据: