SQLクエリの概要②

1.お問い合わせ

1.1グループ化するクエリ

select fieldName
from tbName 
where condition_ 
group by 分组要求;
-- group by是一个分组关键字

-- 查询各部门人数是多少
-- 1. 需要按照department_id进行分组
-- 2. 计数需要使用count, 根据用户的employee_id进行计数操作
select department_id, count(employee_id)
from t_employees
group by department_id;

-- 查询各部门的平均工资
-- 1. 需要按照department_id进行分组
-- 2. 平均工资使用avg方法计算
select department_id, avg(salary)
from t_employees
group by department_id;

-- 查询各部门,各岗位的人数
-- 1. 需要按照department_id进行分组
-- 2. 需要按照岗位job_id进行分组
-- 3. 记录人数,count(employee_id)
select department_id, job_id, count(employee_id)
from t_employees
group by department_id, job_id;

-- [42000][1055] Expression #1 of SELECT list is not in GROUP BY
-- clause and contains nonaggregated column 'company.t_employees.department_id'
-- which is not functionally dependent on columns in GROUP BY clause;
-- this is incompatible with sql_mode=only_full_group_by
-- 如果使用group by要求分组字段一定是查询要求字段,这里需要根据查询结果进行分组
select department_id
from t_employees
group by job_id;

1.2パケットフィルタクエリ

select fieldName
from tbName 
where condition_ 
group by 分组要求
having 过滤规则;
-- having是在 group by 之后的条件过滤

-- 查询指定100,50,30,80最高工资
-- 1. 需要按照department_id进行分组
-- 2. 最高工资
-- 3. 限制manager_id = 100
-- 4. 限制department_id号为100,50,30,80
select department_id, max(salary)
from t_employees
where manager_id = 100
group by department_id
having department_id in (100, 50, 30, 80);

1.3限られたお問い合わせ

select fieldName
from tbName
limit 限制;

-- limit [offset_start], row_count

-- 查询员工表中前10个数据,员工first_name, employee_id
select employee_id, first_name
from t_employees
limit 10;

-- 查询员工表中10个数据,要求offset为3,员工first_name, employee_id
-- 起始行从0开始
select employee_id, first_name
from t_employees
limit 3,10;

-- 【重点】
-- limit核心用法,分页查询
-- pageCount 当前是第几页
-- itemCount 一页展示多少个元素
-- select * from tbName limit (pageCount - 1) * itemCount, itemCount;

-- 展示第一页10条数据
select employee_id, first_name
from t_employees
limit 0, 10;

-- 展示第二页10条数据
select employee_id, first_name
from t_employees
limit 10, 10;

-- 展示第三页10条数据
select employee_id, first_name
from t_employees
limit 20, 10;

1.4サブクエリ

1.4.1基本フォーマット
select fieldName
from tbName
where (子查询结果);
結果として1.4.2サブクエリ制約条件決意
-- 查询工资高于Jack的员工id和姓名
-- 1. 找出Jack的工资
-- 2. 得到Jack工资,作为条件查询对应的员工信息
select salary
from t_employees
where first_name = 'Jack';

select employee_id, first_name
from t_employees
where salary > 8400;

-- 整合为子查询
-- 条件判断
select employee_id, first_name
from t_employees
where salary > (select salary
                from t_employees
                where first_name = 'Jack');
列挙制限など1.4.3サブクエリ結果
-- 查询和Jack同部门的员工信息
-- 1. 找出Jack的部门编号
select department_id
from t_employees
where first_name = 'Jack';

-- 2. 根据Jack的部门编号,使用in枚举查询,限制条件
select employee_id, first_name
from t_employees
where department_id in (80);

-- 整合为子查询
select employee_id, first_name
from t_employees
where department_id in (select department_id
                        from t_employees
                        where first_name = 'Jack');
表として1.4.4サブクエリの結果、指定されたテーブル内からクエリデータ
-- 查询员工表中工资前五名的员工信息
-- 1. 找到员工的id,first_name,工资降序
select employee_id, first_name
from t_employees
order by salary desc;

select employee_id, first_name
from (select employee_id, first_name
      from t_employees
      order by salary desc) as temp
limit 5;

1.5テーブルは、クエリに参加します

1.5.1基本フォーマット
select fieldName
from tbName1
连接符 tbName2
on 条件
インナー1.5.2は、クエリに参加します
-- 查询所有部门部门名,和对应的员工信息id和first_name
select t_departments.department_name,
       t_employees.employee_id,
       t_employees.first_name -- 查询内容
from t_employees -- 从员工表中查询
         inner join t_departments -- 内连接部门表
                    on t_employees.department_id = t_departments.department_id;
-- 条件限制员工表中的部门Id = 部门表中的部门id

-- 查询所有部门部门名,和对应的员工信息id和first_name
-- 给予表格一个别名,方便使用
select d.department_name,
       e.employee_id,
       e.first_name -- 查询内容
from t_employees e-- 从员工表中查询
         inner join t_departments d-- 内连接部门表
                    on e.department_id = d.department_id; -- 条件限制员工表中的部门Id = 部门表中的部门id
-- 查询所有员工对应的ID号,名字,部门名称,和国家对应名字
select te.employee_id, te.first_name, td.department_name, tc.country_name
from t_employees te
    inner join t_departments td on te.department_id = td.department_id
    inner join t_locations tl on td.location_id = tl.location_id
    inner join t_countries tc on tl.country_id = tc.country_id;
-- 查询所有员工对应的ID号,名字,工作职称,部门名称,和国家对应名字
select te.employee_id, te.first_name, tj.job_title, td.department_name, tc.country_name
from t_employees te
    inner join t_jobs tj on te.job_id = tj.job_id
    inner join t_departments td on te.department_id = td.department_id
    inner join t_locations tl on td.location_id = tl.location_id
    inner join t_countries tc on tl.country_id = tc.country_id;	
1.5.3左外部結合
-- 左外连接 左表是主表,要求左表完整显示,右表匹配左表数据,如果右表没有数据匹配,显示null

-- 查询所有的员工信息ID,first_ame,已经对应的部门名字
select te.employee_id, te.first_name, td.department_name
from t_employees te
         left join t_departments td on te.department_id = td.department_id;
1.5.4右外側コネクタ
-- 右外连接查询,右表是主表,要求右表完整展示,左表匹配右表数据,如果左表没有数据匹配,显示null

-- 查询所有部门对应员工信息,员工信息没有显示null
select td.department_name,te.employee_id, te.first_name
from t_employees te
         right join t_departments td on te.department_id = td.department_id;
公開された25元の記事 ウォン称賛78 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_41424681/article/details/104995324