Sql 在外部查询中显示子查询的计数
我希望将Query2中的部门数作为Query1中的一个新列来查看。我该怎么做Sql 在外部查询中显示子查询的计数,sql,oracle,Sql,Oracle,我希望将Query2中的部门数作为Query1中的一个新列来查看。我该怎么做 Query 1: SELECT location_id, street_address, postal_code, city, state_province, country_id FROM locations; Query 2: SELECT location_id, COUNT(department_id) FROM departme
Query 1:
SELECT
location_id,
street_address,
postal_code,
city,
state_province,
country_id
FROM
locations;
Query 2:
SELECT
location_id,
COUNT(department_id)
FROM
departments
group by location_id;
一种方法是相关子查询:
select l.*,
(select count(*) from departments d where d.location_id = l.location_id
) as num_departments
from locations l;
一种方法是相关子查询:
select l.*,
(select count(*) from departments d where d.location_id = l.location_id
) as num_departments
from locations l;