如何查询 EMPLOYEE.EMAIL1 或 EMPLOYEE.EMAIL2 没有匹配的条目?

发布于 2024-10-13 04:29:33 字数 1715 浏览 1 评论 0原文

继续我在上发布的问题 我的用例解释可以在单个查询中解决而不影响性能吗?,我现在有一个不同的要求,我想知道是否可以在单个查询中再次解决此问题而不影响性能。我的新要求解释如下:

涉及 2 个表:

Table 1: EMPLOYEE (column1, column2, email1, email2, column5, column6)
Table 2: EMAILLIST (email)

我的新要求是,如果 EMPLOYEE.EMAIL1 和 EMPLOYEE.EMAIL2 在 EMAILLIST 表中都有匹配的条目,则应忽略该行。因此,如果 EMPLOYEE.EMAIL1 或 EMPLOYEE.EMAIL2 没有匹配的条目(这意味着至少其中一列将有匹配的条目),我希望应该返回这一行。因为任一电子邮件都没有匹配的条目,所以我需要在 SELECT 子句本身中标识哪些电子邮件(email1 或 email2)在 EMAILLIST 表中没有匹配的条目。

示例:

INSERT INTO EMAILLIST (email) VALUES ('[email protected]');
INSERT INTO EMPLOYEE (column1, column2, email1, email2, column5, column6) VALUES ('c1', 'c2', '[email protected]', '[email protected]', 'c5', 'c6');

我希望在查询输出中返回一些标识,其中一封电子邮件没有这样的匹配条目:

EMAIL1           | EMAIL2            | EMAIL1MATCHED | EMAIL2MATCHED
[email protected] | [email protected]  | Yes           | No

我们正在使用 PostgreSQL v8.2.3,如果有关系的话。

更新: 我的表大小有点大:EMPLOYEE 有 165018 条记录,EMAILLIST 有 1810228 条记录

In continuation to a question posted by me on
Can my use case explained be solved in a single query without affecting performance?, I've a different requirement now and I wonder whether this could be again solved in a single query without affecting performance. My new requirement explained below:

There are 2 tables involved:

Table 1: EMPLOYEE (column1, column2, email1, email2, column5, column6)
Table 2: EMAILLIST (email)

My new requirement is, if both EMPLOYEE.EMAIL1 and EMPLOYEE.EMAIL2 have a matching entry in EMAILLIST table, then this row should be ignored. Hence, if either EMPLOYEE.EMAIL1 or EMPLOYEE.EMAIL2 do not have a matching entry (which means atleast one of the column will have a matching entry), I expect this row should be returned. Because either one of the email do not have a matching entry, I need a identification in SELECT clause itself on which email (email1 or email2) did not have a matching entry in EMAILLIST table.

Example:

INSERT INTO EMAILLIST (email) VALUES ('[email protected]');
INSERT INTO EMPLOYEE (column1, column2, email1, email2, column5, column6) VALUES ('c1', 'c2', '[email protected]', '[email protected]', 'c5', 'c6');

I expect to return with some identification in query output on which one of the email do not have a matching entry like this:

EMAIL1           | EMAIL2            | EMAIL1MATCHED | EMAIL2MATCHED
[email protected] | [email protected]  | Yes           | No

We're using PostgreSQL v8.2.3, if it matters.

UPDATE: My table size are little huge: EMPLOYEE has got 165018 records and EMAILLIST has got 1810228 records

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

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

发布评论

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

评论(2

好听的两个字的网名 2024-10-20 04:29:33

如果 emaillistemail 列已建立索引,则应该没问题:

 SELECT employee.email1, employee.email2,
        e1.email IS NOT NULL AS email1_matched, e2.email IS NOT NULL AS email2_matched
   FROM employee
   LEFT JOIN emaillist e1 ON e1.email = employee.email1
   LEFT JOIN emaillist e2 ON e2.email = employee.email2
 WHERE e1.email IS NULL OR e2.email IS NULL

Provided that the email column of emaillist is indexed, this should be fine:

 SELECT employee.email1, employee.email2,
        e1.email IS NOT NULL AS email1_matched, e2.email IS NOT NULL AS email2_matched
   FROM employee
   LEFT JOIN emaillist e1 ON e1.email = employee.email1
   LEFT JOIN emaillist e2 ON e2.email = employee.email2
 WHERE e1.email IS NULL OR e2.email IS NULL
笑梦风尘 2024-10-20 04:29:33
SELECT  *
FROM    (
        SELECT  email1, email2,
                email1 IN
                (
                SELECT  email
                FROM    emaillist
                ) AS email1matched,
                email2 IN
                (
                SELECT  email
                FROM    emaillist
                ) AS email2matched
        FROM    employee
        ) q
WHERE   email1matched <> email2matched
SELECT  *
FROM    (
        SELECT  email1, email2,
                email1 IN
                (
                SELECT  email
                FROM    emaillist
                ) AS email1matched,
                email2 IN
                (
                SELECT  email
                FROM    emaillist
                ) AS email2matched
        FROM    employee
        ) q
WHERE   email1matched <> email2matched
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文