简体   繁体   English

如何查询具有相同字段的两个表,仅当字段值相同时才返回 id

[英]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.

相关问题 如何从两个表中选择两个在同一个字段中具有相同值的行? - How to select rows from two tables where both have the same value in the same field? 加入两个基于相同ID的表格字段? - Join two tables field based on the same id? 如何一次查询两个表以查找具有相同名称但数据不同的字段? - How to query two tables at once for a field with the same name but different data? 如何区分选择查询中两个表的相同字段名称? - How to differentiate between same field names of two tables in a select query? 在连接查询到表字段有同名意味着如何获取两个字段的值? - In join query to table field have same name means how get the two fields value? 在一个字段中具有相同数据单元值但在另一个字段中具有不同数据的两条记录,根据条件查询仅返回一个 - Two records that have same data cell value in one field but different in another, query to return only one based on criteria 如何将具有相同字段的两个表的结果连接到一个字段中? - How to join result from two tables with same field into one field? 显示来自具有相同字段的两个表的数据 - display data from two tables that have the same field 从两个不同的表MYSQL查询相同的字段 - Query same field from two different tables MYSQL 如何将两个值的where子句放在同一字段中? - How to where clause of two value in the same field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM