【问题标题】:Joining two tables in Oracle sql在 Oracle sql 中连接两个表
【发布时间】:2012-01-08 01:41:46
【问题描述】:

我有 2 张桌子。其中一个表有 7 个值,另一个表有 5 个值。这些表有它们共同的主键。我想以这种方式加入两个表: 如果我有一张桌子

English              French
-------------------- --------------------
one                  Un
two                  Deux
three                Trois
four                 Quatre
four                 Quattro
five                 Cinq
five                 Cinco

还有一个:

English              French
-------------------- --------------------
one                  aaaaa
two                  bbbbb
three                ccccc
four                 
five

我想要一张这样的桌子:

English              French
-------------------- --------------------
one                  Un
one                  aaaaa
two                  Deux
two                  bbbb
three                Trois
three                ccccc
four                 Quatre
four                 Quattro
four                 --------
five                 Cinq
five                 Cinco
five                 ----------

我尝试使用 join,但它对值 fourfive 进行了线性组合。我该怎么做呢?谢谢。

编辑:SQL查询:

SELECT l.date_location, l.duree,  r.km_restitution, r.km_parcouru 
FROM  locations l, restitutions r
UNION
SELECT l.num_client, l.date_location, l.duree,  r.km_restitution, r.km_parcouru
FROM  locations l, restitutions r


id_agence num_immatriculation num_client km_restitution km_parcouru state date_restitution 
1   406BON69    1002    30000   1000    BON 29-MAY-10
3   785CIM13    1001    56580   80  BON 09-AUG-08
5   800BBB75    1000    2020    20  BON 24-APR-11
4   307VXN78    1000    20040   40  BON 28-JAN-11
2   290UTT92    1004    30030   30  BON 01-AUG-10
5   777SET13    1005    4030    30  BON 26-APR-11
2   179CLV92    1004    15015   15  BON 03-FEB-11
5   400AAA75    1003    1020    20  BON 18-SEP-11
5   666NEF69    1004    3040    40  BON 15-APR-11
2   111AAA75    1001    20020   20  BON 21-DEC-09
1   333CCC78    1001    43250   40  BON 27-DEC-09
2   260CDE95    1003    79000   430 BON 10-SEP-09
4   307VXN78    1003    20090   90  BON 11-FEB-11
1   123ABC78    1003    10010   10  BON 04-OCT-10
1   222BBB77    1001    9050    50  BON 23-DEC-09


Locations
    id_agence num_immatricul  num_client duree   date_location
    2   406BON69    1002    20  10-MAY-10
    3   785CIM13    1001    3   07-AUG-08
    5   800BBB75    1000    7   18-APR-11
    4   307VXN78    1000    5   24-JAN-11
    1   290UTT92    1004    1   31-JUL-10
    5   777SET13    1005    4   23-APR-11
    1   179CLV92    1004    5   30-JAN-11
    5   400AAA75    1003    2   17-SEP-11
    2   123ABC78    1003    4   01-OCT-10
    5   666NEF69    1004    5   11-APR-11
    1   111AAA75    1001    2   20-DEC-09
    1   222BBB77    1001    2   22-DEC-09
    1   333CCC78    1001    3   25-DEC-09
    1   260CDE95    1003    10  01-SEP-09
    4   307VXN78    1003    13  30-JAN-11
    2   123ABC78    1003    8   20-NOV-11
    2   406BON69    1002    10  20-NOV-11

想要的结果

id_agence num_immatricul  num_client duree   date_location date_restitution
2   406BON69    1002    20  10-MAY-10     date_restitution
3   785CIM13    1001    3   07-AUG-08     date_restitution
5   800BBB75    1000    7   18-APR-11     date_restitution
4   307VXN78    1000    5   24-JAN-11     date_restitution
1   290UTT92    1004    1   31-JUL-10     date_restitution
5   777SET13    1005    4   23-APR-11     date_restitution
1   179CLV92    1004    5   30-JAN-11     date_restitution
5   400AAA75    1003    2   17-SEP-11     date_restitution
2   123ABC78    1003    4   01-OCT-10     date_restitution
5   666NEF69    1004    5   11-APR-11     date_restitution
1   111AAA75    1001    2   20-DEC-09      date_restitution
1   222BBB77    1001    2   22-DEC-09     date_restitution
1   333CCC78    1001    3   25-DEC-09     date_restitution
1   260CDE95    1003    10  01-SEP-09     date_restitution
4   307VXN78    1003    13  30-JAN-11     date_restitution
2   123ABC78    1003    8   20-NOV-11      ----------------
2   406BON69    1002    10  20-NOV-11      ---------------

除了列名,我在其中放置 date_restitution 包含真实日期。

【问题讨论】:

  • 恕我直言,union all 会帮助你
  • @mkab,你能包括两个表的结构吗?另外,您确定要在位置和恢复原状之间进行笛卡尔连接吗?
  • 好的,让我添加它。不过这需要一些时间
  • @MarkBannister:我已经添加了两个表
  • @mkab:为什么不在您的问题中发布您想要的结果?

标签: sql oracle join


【解决方案1】:

你实际上需要一个联合:

SELECT English, French FROM T1
UNION
SELECT English, French FROM T2

如果你不关心重复,你可以使用 UNION ALL

在 OP 评论后编辑:

SELECT l.num_client, l.id_agence, l.num_immatricul
FROM  locations l
UNION
SELECT r.num_client, r.id_agence, r.num_immatriculation
FROM  restitutions r

【讨论】:

  • 我有一个错误query block has incorrect number of result columns
  • @mkab,从查询的第二部分中删除 l.num_client。我很惊讶您不需要以某种方式加入位置和恢复?
  • @mkab 您的 SQL 显示您正在尝试将顶部语句中的 4 列与底部语句中的 5 列合并。 query block has incorrect number of result columns 表示您的两个语句不能合并在一起,因为它们每个都包含不同数量的结果列。尝试从底部语句中删除 l.num_client 或将其添加到顶部语句中。
  • @mkab 如果删除 num_client,每条记录都会重复,我的猜测是必须加入位置和恢复原状,它们是否共享密钥?
  • @wweicker,我一开始也是这么想的,但由于表格相同,他最终只会得到重复的结果
【解决方案2】:

您可以使用 UNION:

select English, French from Table1
UNION ALL
select English, French from Table2

或完全外部连接

select distinct coalesce(T1.English, T2.English), coalesce(T1.French, T2.French)
from Table1 T1
full outer join Table2 T2 on T1.English = T2.English

编辑:

假设您希望 restitutions.date_restitution 出现在 date_location 的位置以进行归还记录 -

SELECT l.num_client, l.date_location, l.duree,  to_number(null) km_restitution, to_number(null) km_parcouru
FROM  locations l
UNION ALL
SELECT r.num_client, r.date_restitution date_location, 0 duree,  r.km_restitution, r.km_parcouru
FROM  restitutions r

进一步编辑(基于提供的结果):

select l.id_agence,
       l.num_immatricul,
       l.num_client,
       l.duree,
       l.date_location,
       decode(r.date_restitution, NULL,'----------------', 'date_restitution')
           as date_restitution -- or just r.date_restitution
from location l
left outer join restitution r
on l.id_agence = r.id_agence and 
   l.num_immatricul = r.num_immatricul and 
   l.num_client = r.num_client and 
   l.date_location <= r.date_restitution

【讨论】:

  • 我使用UNION ALL时出错:query block has incorrect number of result columns
  • @mkab:您需要在两个联合查询中使用相同数量(和类型)的列 - 此处的查询应该可以工作,但如果您已修改它们以匹配您的实际表,那​​么您可能已经引入了查询结构之间的差异。您能否包含您尝试运行的实际查询?
  • SELECT l.date_location, l.duree, r.km_restitution, r.km_parcouru FROM locations l, restitutions r UNION SELECT l.num_client, l.date_location, l.duree, r.km_restitution, r.km_parcouru FROM locations l, restitutions r
  • @mkab,试试SELECT l.num_client, l.date_location, l.duree, r.km_restitution, r.km_parcouru FROM locations l, restitutions r UNION ALL SELECT l.num_client, l.date_location, l.duree, r.km_restitution, r.km_parcouru FROM locations l, restitutions r
  • @mkab:不,您的推理完全错误 - union 将一个数据集中的所有值添加到另一个数据集中的所有值,因此您希望组合数据集中有 32 行。这就是您最初的问题所要求的 - 添加 7 行和 5 行以产生 12 行。
【解决方案3】:

以下应该做到这一点。

SELECT tab1.English, tab1.French
 UNION
SELECT tab2.English, tab2.French

【讨论】:

  • 我有一个错误:query block has incorrect number of result columns
【解决方案4】:

对于可能有同样问题的其他读者。根据我对这个问题的经验,加入表位置和恢复是一个好主意,因为它们都具有几乎相同的属性和数据。我最终决定更改我的数据库并创建一个包含位置和恢复属性的新表,并将一些不可用的值设置为NULL。这将减少表之间的大量joins,并且查询将更易于处理。

【讨论】:

    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多