[英]How to create a table where one column contains a same value for all rows with different id?(See Layout for clarity)
[英]Return all rows that have the same id based on a value on one of the rows of a different column
我想根据不同列的行之一上的值返回 columnA 上具有相同 id 的所有行,例如 ColumnB。 连接表结果如下所示,我想返回所有没有至少一个我要查找的值实例的结果。
在下面的示例中,我想列出所有根本没有联系过的组织,这意味着在 ConStatus 列中没有值“已联系”的组织名称或 ID。
您可以通过使用Subquery
来实现这一点
SELECT *
FROM table
WHERE OrgId NOT IN (
SELECT DISTINCT OrgId
FROM table
WHERE ConStatus = 'Contacted'
)
子查询将获取所有联系的组织 ID。 然后将使用NOT IN
排除这些 ID,以获得从未联系过的 ID。
您可以尝试使用not exists
相关子查询
select * from tablename a
where not exists
(select 1 from tablename b where a.orgid=b.orgid and ConStatus = 'Contacted')
试试这个。
SELECT *
FROM table
WHERE OrgId NOT IN (
SELECT OrgId
FROM table
WHERE ConStatus = 'Contacted'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.