从子查询中打印数据 SQL

Printing data from sub-queries SQL

我有 2 个表:SalesPeopleCustomers 分别以 snumcnum 作为主键;两个表都有 city 列。

如果不使用联接,我们必须告诉属于同一城市的客户和销售人员的姓名。

我已经使用嵌套查询来打印属于客户所在城市的销售人员,但无法弄清楚如何用它打印客户姓名。

SELECT S.* 
FROM SalesPeople S
WHERE City IN(
    SELECT City 
    FROM Customers CX
    );

这个怎么样? (忽略 WITH 分解子句在 Oracle 9i 中不存在的事实(至少,我认为是这样);您已经有了那些表)。

示例数据:

SQL> with
  2  salespeople (snum, city) as
  3    (select 1, 'London' from dual union all
  4     select 2, 'Paris'  from dual union all
  5     select 3, 'Rome'   from dual
  6    ),
  7  customers (cnum, city) as
  8    (select 100, 'Zagreb' from dual union all
  9     select 101, 'Rome'   from dual union all
 10     select 102, 'Rome'   from dual union all
 11     select 103, 'Paris'  from dual
 12    )

查询:

 13  select person_num
 14  from (select snum as person_num, city from salespeople
 15        union
 16        select cnum, city from customers
 17       )
 18  where city = 'Rome';

PERSON_NUM
----------
         3
       101
       102

SQL>