mysql 中with的用法
1、案例一:
建表:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary INT
);
INSERT INTO employees (employee_id, first_name, last_name, salary)
VALUES
(1, 'John', 'Doe', 50000),
(2, 'Jane', 'Doe', 55000),
(3, 'Jim', 'Smith', 60000),
(4, 'Sarah', 'Johnson', 65000),
(5, 'Tom', 'Brown', 70000);
select * from employees e
构建临时表:
with moth_saleary as(
select e.employee_id ,e.first_name ,
e.last_name ,e.salary ,salary/12 as moth_saleary from employees e
)
select * from moth_saleary
#salary/12 as moth_saleary 这个可以和构建的临时表名称一样,也可以不一样,这个临时表只是将查询结果包了起来。作为结果展示了出来
查询结果: