[英]How to query two tables that have the same field, that returns id only if field value is the same
I have two tables (email and phone) that indicate if a message was opened (read/listened).我有两个表(电子邮件和电话)指示消息是否被打开(阅读/收听)。 I need to find the id's of the member's who have not opened the message (they could have been sent an email and called).
我需要找到尚未打开消息的成员的 ID(他们可能已收到电子邮件并已致电)。 So the only case in which they did not receive the message is if they did not open the email or answer the phone.
因此,他们没有收到消息的唯一情况是他们没有打开电子邮件或接听电话。
I have a query that looks like this to know if they opened it:我有一个看起来像这样的查询,以了解他们是否打开了它:
SELECT person_id, last_name, first_name
FROM person
WHERE person_id IN (
SELECT DISTINCT person_id
FROM (
SELECT person_id
FROM msg_email WHERE message_id = ? AND opened = 'Y'
UNION ALL
SELECT person_id
FROM msg_voice WHERE message_id = ? AND opened = 'Y') tt
)
ORDER BY last_name ASC, first_name ASC"
However, this only works for knowing if it was opened via either of the delivery methods.但是,这仅适用于了解它是否是通过任何一种交付方式打开的。
How would I fashion a query to find the person ids of those that received the message (exists is one of the two tables), but the opened value is 'N' exclusively in both tables?我将如何设计查询以查找收到消息的人员的人员 ID(存在是两个表之一),但打开的值在两个表中都是“N”?
Sample Data person table示例数据人员表
person_id firstname lastname
1 Joe Smith
2 Tom Jones
msg_email table msg_email 表
message_id person_id opened
1 1 N
1 2 Y
msg_phone table msg_phone 表
message_id person_id opened
1 1 N
1 2 N
So I need a query that will only return Joe Smith所以我需要一个只返回 Joe Smith 的查询
Hmmm, one method is a series of EXISTS
/ IN
conditions:嗯,一种方法是一系列
EXISTS
/ IN
条件:
SELECT p.person_id, p.last_name, p.first_name
FROM person p
WHERE EXISTS (SELECT 1
FROM msg_email e
WHERE e.person_id = p.person_id AND e.opened = 'N'
) AND
NOT EXISTS (SELECT 1
FROM msg_email e
WHERE e.person_id = p.person_id AND e.opened = 'Y'
) AND
EXISTS (SELECT 1
FROM msg_voice v
WHERE v.person_id = p.person_id AND v.opened = 'N'
) AND
NOT EXISTS (SELECT 1
FROM msg_voice v
WHERE v.person_id = p.person_id AND v.opened = 'Y'
)
ORDER BY last_name ASC, first_name ASC;
I recommend EXISTS
over IN
because it usually has better performance.我推荐
EXISTS
不是IN
因为它通常具有更好的性能。 That will be true if you have indexes on msg_email(person_id, opened)
and msg_voice(person_id, opened)
.如果您在
msg_email(person_id, opened)
和msg_voice(person_id, opened)
上有索引,那将是正确的。
EDIT:编辑:
It occurs to me that you want 'N'
in either table along with no 'Y'
in both tables.我突然想到,您希望在任一表中都有
'N'
,而在两个表中都没有'Y'
。 The logic is similar but:逻辑是相似的,但:
SELECT p.person_id, p.last_name, p.first_name
FROM person p
WHERE (EXISTS (SELECT 1
FROM msg_email e
WHERE e.person_id = p.person_id AND e.opened = 'N'
) OR
EXISTS (SELECT 1
FROM msg_voice v
WHERE v.person_id = p.person_id AND v.opened = 'N'
)
) AND
NOT EXISTS (SELECT 1
FROM msg_email e
WHERE e.person_id = p.person_id AND e.opened = 'Y'
) AND
NOT EXISTS (SELECT 1
FROM msg_voice v
WHERE v.person_id = p.person_id AND v.opened = 'Y'
)
ORDER BY last_name ASC, first_name ASC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.