如何查询 EMPLOYEE.EMAIL1 或 EMPLOYEE.EMAIL2 没有匹配的条目?
继续我在上发布的问题 我的用例解释可以在单个查询中解决而不影响性能吗?,我现在有一个不同的要求,我想知道是否可以在单个查询中再次解决此问题而不影响性能。我的新要求解释如下:
涉及 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
emaillist
的email
列已建立索引,则应该没问题:Provided that the
email
column ofemaillist
is indexed, this should be fine: