如何使用并集

时间:2019-06-02 12:31:02

标签: sql oracle11g oracle10g

在单个表格中显示销售人员的ID和名称以及产品ID和类别。通过添加附加列TYPE来指示结果中的行的来源,附加列TYPE的可能值为“ S”(推销员)和“ P”(乘积)。显示所有行。

在不使用alter的情况下,我不会添加列“ type”

select sid AS ID,sname from salesman
union
select category as "DETAILS" from product

实际表是

推销员表

SID   SNAME    LOCATION
1     Peter    London
2     Michael  Paris
3     John     Mumbai
4     Harry    Chicago
5     Kevin    London
6     Alex     Chicago

产品表

PRODID   PDESC           CATEGORY       DISCOUNT
101      Basketball      Sports         5
102      Shirt           Apparel        10
103      NULL            Electronics    15
104      CricketBat      Sports         20
105      Trouser         Apparel        5
106      Television      ELECTRONICS    20

预期产量

TYPE  ID       DETAILS
P     101      Sports
P     102      Apparel
P     103      Electronics
P     104      Sports
P     105      Apparel
P     106      ELECTRONICS

1 个答案:

答案 0 :(得分:1)

您想要这样的结果吗?

TYPE ID   DETAILS
S    1    Peter
S    2    Michael
S    3    John
S    4    Harry
S    5    Kevin
S    6    Alex
P    101  Sports
P    102  Apparel
P    103  Electronics
....

您可以尝试这样查询

select 'S' AS TYPE, sid AS ID,SNAME AS DETAILS from Salesman
union
select 'P' As TYPE, PRODID, category as "DETAILS" from Product