笛卡尔积sql语句使用方式

时间:2024-02-01 07:36:38

笛卡尔积(cross join)

--1.笛卡尔积方式一
select 
a.id,
b.code,
c.name,
d.addr
from 表1 a
cross join 表2 b
cross join 表3 c
left join 表4 e on d.id = a.id and d.code=b.code and d.name=c.name 
where a.system_type=1 
and b.system_type=2 
and c.system_type=3 
order by b.department_id


--1.笛卡尔积方式二
select 
a.id,
b.code,
c.name,
d.addr
from 表1 a
inner join 表2 b on b.system_type=2
inner join 表3 c on c.system_type=3
left join 表4 e on d.id = a.id and d.code=b.code and d.name=c.name 
where a.system_type=1 
order by b.department_id

以上2中方式都可以。