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