MySQL - 表达式With as 语句的使用及练习

2023-12-13 12:26:45

目录

8.1 WITH AS 的含义

8.2 WITH AS语法的基本结构如下:

8.3 练习题1

8.4 牛客练习题


8.1 WITH AS 的含义

WITH AS 语法是MySQL中的一种临时结果集,它可以在SELECT、INSERT、UPDATE或DELETE语句中使用。通过使用WITH AS语句,可以将一个查询的结果存储在一个临时表中,然后在后续的查询中引用这个临时表。这样可以简化复杂的查询,提高代码的可读性和可维护性。

8.2 WITH AS语法的基本结构如下:

WITH temporary_table_name AS (
  SELECT column1, column2, ...
  FROM table_name
  WHERE condition
)
SELECT * FROM temporary_table_name;
其中,temporary_table_name是临时表的名称,table_name是要查询的表名,condition是筛选条件。

8.3 练习题1

数据库

drop table if exists  `dept_emp` ;
drop table if exists  `salaries` ;
CREATE TABLE `dept_emp` (
                            `emp_no` int(11) NOT NULL,
                            `dept_no` char(4) NOT NULL,
                            `from_date` date NOT NULL,
                            `to_date` date NOT NULL,
                            PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
                            `emp_no` int(11) NOT NULL,
                            `salary` int(11) NOT NULL,
                            `from_date` date NOT NULL,
                            `to_date` date NOT NULL,
                            PRIMARY KEY (`emp_no`,`from_date`));
INSERT INTO dept_emp VALUES(10001,'d001','1986-06-26','9999-01-01');
INSERT INTO dept_emp VALUES(10002,'d001','1996-08-03','9999-01-01');
INSERT INTO dept_emp VALUES(10003,'d004','1995-12-03','9999-01-01');
INSERT INTO dept_emp VALUES(10004,'d004','1986-12-01','9999-01-01');
INSERT INTO dept_emp VALUES(10005,'d003','1989-09-12','9999-01-01');
INSERT INTO dept_emp VALUES(10006,'d002','1990-08-05','9999-01-01');
INSERT INTO dept_emp VALUES(10007,'d005','1989-02-10','9999-01-01');
INSERT INTO dept_emp VALUES(10009,'d006','1985-02-18','9999-01-01');
INSERT INTO dept_emp VALUES(10010,'d006','2000-06-26','9999-01-01');

INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01');
INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01');
INSERT INTO salaries VALUES(10003,43311,'2001-12-01','9999-01-01');
INSERT INTO salaries VALUES(10004,74057,'2001-11-27','9999-01-01');
INSERT INTO salaries VALUES(10005,94692,'2001-09-09','9999-01-01');
INSERT INTO salaries VALUES(10006,43311,'2001-08-02','9999-01-01');
INSERT INTO salaries VALUES(10007,88070,'2002-02-07','9999-01-01');
INSERT INTO salaries VALUES(10009,95409,'2002-02-14','9999-01-01');

有一个员工表dept_emp简况如下:

有一个薪水表salaries简况如下:

问题1:获取每个部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary,按照部门编号dept_no升序排列,以上例子输出如下:

with temp as (
    select dept_no,d.emp_no,salary
    from dept_emp as d
    inner join salaries as s
    on d.emp_no=s.emp_no
)
select temp.dept_no,emp_no,maxSalary
from temp
inner join(
select dept_no, max(salary) as maxSalary
from temp
group by dept_no) as t2
on temp.dept_no=t2.dept_no and t2.maxSalary=salary
order by temp.dept_no;

8.4 牛客练习题

SQL206 获取每个部门中当前员工薪水的最高的相关信息

文章来源:https://blog.csdn.net/weixin_40968325/article/details/134886097
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。