mysql版本在8.0之前不能使用with的写法。
1.with 写法,让两个表的相同等级人数相除
统计出两张表每个等级的人数,再使用等级进行join,再计算。
with t1 as (
select
emp.emp_level as type,
count(emp.id) as n
from
emp
group by
emp.emp_level
),
t2 as (
select
d.depot_level as type,
count(d.id) as n
from
depot as d
group by
d.depot_level
)
select
t1.n/t2.n
from
t1
left join
t2
on t1.type = t2.type
2.这个写法等同于
3.后面
特别是从多张表中取数据时,而且每张表的数据量又很大时,使用with写法可以先筛选出来一张数据量较少的表,避免全表join。
mysql版本在8.0之前不能使用with的写法。