简体   繁体   English

根据不同列的行之一上的值返回具有相同 id 的所有行

[英]Return all rows that have the same id based on a value on one of the rows of a different column

I would like to return all rows that have the same id on columnA based on a value on one of the rows of a different column say ColumnB.我想根据不同列的行之一上的值返回 columnA 上具有相同 id 的所有行,例如 ColumnB。 The joined table results look like the below, I would like to return all results that do not have at least one instance of the value I am looking for.连接表结果如下所示,我想返回所有没有至少一个我要查找的值实例的结果。

In the example below, I would like to list all organizations that have not been contacted at all which means the organizationname or id which does not have the value 'Contacted' in the ConStatus column.在下面的示例中,我想列出所有根本没有联系过的组织,这意味着在 ConStatus 列中没有值“已联系”的组织名称或 ID。

enter image description here在此处输入图片说明

You can achieve this by using Subquery您可以通过使用Subquery来实现这一点

SELECT * 
FROM table
WHERE OrgId NOT IN (
     SELECT DISTINCT OrgId
     FROM table
     WHERE ConStatus = 'Contacted'
)

The subquery will get all organization IDs which are contacted.子查询将获取所有联系的组织 ID。 These IDs will then be excluded using NOT IN to get the ones which are never contacted.然后将使用NOT IN排除这些 ID,以获得从未联系过的 ID。

You can try using correlated subquery with not exists您可以尝试使用not exists相关子查询

select * from tablename a
 where not exists 
     (select 1 from tablename b where a.orgid=b.orgid and ConStatus = 'Contacted')

Try this one.试试这个。

SELECT * 
    FROM table
    WHERE OrgId NOT IN (
         SELECT  OrgId
         FROM table
         WHERE ConStatus = 'Contacted'
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个表,其中一列包含具有不同ID的所有行的相同值?(有关说明,请参见布局) - How to create a table where one column contains a same value for all rows with different id?(See Layout for clarity) 根据出现在另一张表的一列中的ID选择一个表中的行,并根据另一列具有多个匹配项 - Select rows in one table based on an id that appears in a column on a different table AND have multiple matches based on another column mysql将具有相同列值的两行连接起来,并计算某些列值,并在一行中返回所有行 - mysql join two rows with same column value and calculate certain column values and return all rows in one row 根据其他列值返回具有相同列值的所有行 - Returns all rows which have same column value based on other column value MySQL-返回不同的ID,其中相同ID的所有行都没有特定的字段值 - MySQL - Return distinct IDs where all rows for the same ID do not have specific field value MySQL-如何返回不同的ID,其中相同ID的所有行的字段值均为空 - MySQL - How to return distinct IDs where all rows for the same ID have null field value 返回一列中值相等而另一列中值不同的行 - Return rows that have the equal values in a column but a different value in another column 获取不同列中具有相同值的所有行的列值之和 - Get the sum of a column value for all rows with same value in different column MySQL:返回具有相同ID的所有行,但按不同字段过滤 - MySQL: Return all rows with same ID but filter by a different field 如何在一个单元格中存储具有相同名称但不同ID的行值? - how to store in one cell rows value what have the same name but different id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM