PL/SQL - 如何从连接表返回单行

发布于 2024-10-14 18:46:07 字数 533 浏览 3 评论 0原文

这可能很简单,我只是目前只见树木不见森林。在 Oracle 中,我根据表 A 的主键从表 A 中选择连接到表 B 的记录。但是表 B 可以有多个与表 A 的主键匹配的记录。这导致我的查询从表中返回重复的行答:下面是我的查询的简化版本:

TableA                TableB
_______               _________
1, Sec1                2, 11/01/2011
2, Sec2                2
3, Sec3                5, 10/01/2011
4, Sec4                6, 10/01/2011

Select A.SecID, A.SecName, B.DateSent from tableA A   
  inner join tableB B on A.SecID = B.SecID

这会返回 Sec2 的 2 条记录 - 我怎样才能让它只返回 Sec2 的 1 条记录?我尝试过使用 unique 和 unique 但仍然得到相同的结果。

this might be quite simple I'm just not seeing the wood for the trees at the moment. In Oracle I'm selecting records from table A that joins to table B based on the primary key of table A. However table B can have multiple records matching the primary key of table A. This is causing my query to return duplicate rows from table A. Below is a cut down version of my query:

TableA                TableB
_______               _________
1, Sec1                2, 11/01/2011
2, Sec2                2
3, Sec3                5, 10/01/2011
4, Sec4                6, 10/01/2011

Select A.SecID, A.SecName, B.DateSent from tableA A   
  inner join tableB B on A.SecID = B.SecID

This is returning 2 records for Sec2 - how can I get it to return only 1 record for Sec2? I've tried using distinct and unique but still get the same results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

峩卟喜欢 2024-10-21 18:46:07
SELECT  secid, secname
FROM    tableA
WHERE   secid IN
        (
        SELECT  secid
        FROM    tableb
        )

如果您还需要 tableB 中的记录:

SELECT  secid, secname, datesent
FROM    (
        SELECT  a.secid, a.secname, b.datesent, ROW_NUMBER() OVER (PARTITION BY a.secid ORDER BY b.datesent DESC) AS rn
        FROM   tableA a
        JOIN   tableB b
        ON     b.secid = a.secid
        )
WHERE   rn = 1

ORDER BY 子句控制您将获得 b 上的多条记录中的哪一条。

SELECT  secid, secname
FROM    tableA
WHERE   secid IN
        (
        SELECT  secid
        FROM    tableb
        )

If you need a record from tableB as well:

SELECT  secid, secname, datesent
FROM    (
        SELECT  a.secid, a.secname, b.datesent, ROW_NUMBER() OVER (PARTITION BY a.secid ORDER BY b.datesent DESC) AS rn
        FROM   tableA a
        JOIN   tableB b
        ON     b.secid = a.secid
        )
WHERE   rn = 1

ORDER BY clause controls which of the multiple records on b will you get.

梨涡少年 2024-10-21 18:46:07

您可以使用 GROUP 函数仅选择一行:

SELECT A.SecID, A.SecName, max(B.DateSent) DateSent
  FROM tableA A   
  JOIN tableB B on A.SecID = B.SecID
 GROUP BY A.SecID, A.SecName

You can use a GROUP function to select only one row:

SELECT A.SecID, A.SecName, max(B.DateSent) DateSent
  FROM tableA A   
  JOIN tableB B on A.SecID = B.SecID
 GROUP BY A.SecID, A.SecName
鯉魚旗 2024-10-21 18:46:07
SELECT DISTINCT a.secid, a.secname
  FROM tableA a, tableB b
 WHERE a.secid = b.secid;
SELECT DISTINCT a.secid, a.secname
  FROM tableA a, tableB b
 WHERE a.secid = b.secid;
别忘他 2024-10-21 18:46:07

建议的解决方案非常好。在某些情况下,您可能会采取稍微不同的方法,特别是当其中一个表与另一个表相比非常大并且表 B 中的外键列上没有索引时。

The solutions suggested are very good. There are cases when you may have take a bit different approach specially when one of the tables is very large compared to another one and absence of an index on foreign key column in table B.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文