需要帮助查找一个表中的值但不存在于另一表中,反之亦然

发布于 2024-12-11 19:07:17 字数 262 浏览 0 评论 0原文

我正在使用甲骨文。

我有2个表,每个表都有一个userID和entitlementID。我想选择表 1 中存在但表 2 中不存在的权利,反之亦然。我需要输出如下:

USER_ID| ENTITLEMENT_1 | ENTITLEMENT_2

 33    |    44    |    <NULL>
 54    |    <NULL>|    55
 33    |    <NULL>|    32

I'm using Oracle.

I have 2 tables, each table has a userID and entitlementID. I want to select the entitlements that exist in table 1 but not in table 2 and vice versa. I need the output to look like:

USER_ID| ENTITLEMENT_1 | ENTITLEMENT_2

 33    |    44    |    <NULL>
 54    |    <NULL>|    55
 33    |    <NULL>|    32

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

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

发布评论

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

评论(3

咋地 2024-12-18 19:07:17

如果您查找一个表中存在的权利,而不是另一个表中存在的权利,那么这不仅仅是一个简单的外部联接。

SELECT t1.userid, t1.entitlement_1, NULL AS entitlement_2
  FROM table1 t1
 WHERE NOT EXISTS ( SELECT 1 FROM table2 t2 
                     WHERE t2.userid = t1.userid
                       AND t2.entitlement_2 = t1.entitlement_1 )
UNION ALL
SELECT t2.userid, NULL AS entitlement_1, entitlement_2
  FROM table2 t2
 WHERE NOT EXISTS ( SELECT 1 FROM table1 t1
                     WHERE t1.userid = t2.userid
                       AND t1.entitlement_1 = t1.entitlement_2 )

第一个选择查找 table2 中不存在的 userid 和 entitlement_1 对,第二个选择执行相反的操作。您没有提供有关所涉及表的大量信息,例如 userid 或 (userid, entitlement) 是否是主键或其他内容,因此这可能可以进行优化。

This is not just a simple outer join if you are looking only for those entitlements that exist in one table and not the other.

SELECT t1.userid, t1.entitlement_1, NULL AS entitlement_2
  FROM table1 t1
 WHERE NOT EXISTS ( SELECT 1 FROM table2 t2 
                     WHERE t2.userid = t1.userid
                       AND t2.entitlement_2 = t1.entitlement_1 )
UNION ALL
SELECT t2.userid, NULL AS entitlement_1, entitlement_2
  FROM table2 t2
 WHERE NOT EXISTS ( SELECT 1 FROM table1 t1
                     WHERE t1.userid = t2.userid
                       AND t1.entitlement_1 = t1.entitlement_2 )

The first select finds the userid and entitlement_1 pairs that don't exist in table2, and the second select does the opposite. You didn't give a lot of information about the tables involved, like if userid or (userid, entitlement) is a primary key or anything, so this might be able to be optimized.

耶耶耶 2024-12-18 19:07:17

您需要执行完整的外部联接才能完成此任务。

以下是有关外连接的更多信息:

http://en.wikipedia.org/wiki /Join_(SQL)#Outer_joins

You need to do a full outer join to accomplish this task.

Here is more info on outer joins:

http://en.wikipedia.org/wiki/Join_(SQL)#Outer_joins

路弥 2024-12-18 19:07:17

这应该适合你:

SELECT * FROM Table1 t1
LEFT JOIN 
  (SELECT userID FROM Table2)
  AS t2 ON t1.userID=t2.userID

This should work for you:

SELECT * FROM Table1 t1
LEFT JOIN 
  (SELECT userID FROM Table2)
  AS t2 ON t1.userID=t2.userID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文