[英]How do I use SQL/mySQL to select rows where 2 columns have multiple of the same value and 1 column has a distinct value?
[英]How do I select multiple rows where one row has a specific value in a column?
我有一個數據表,如下所示。 當項目的至少一個提交的狀態包含 16 時,我需要創建一個顯示項目所有提交的 SSRS 報告。
示例數據:
| Project | SubmittalNumber | Description | Status |
|---------|-----------------|-------------|--------|
|53613 |13-1 |Permit |16A |
|53613 |13-2 |Survey |15 |
|53613 |13-3 |Hours |14 |
|53674 |74-1 |Permit |15 |
|53674 |74-2 |Survey |14 |
|1063 |63-1 |Permit |16 |
|1063 |63-2 |Survey |13 |
我希望返回的數據:
| Project | SubmittalNumber | Description | Status |
|---------|-----------------|-------------|--------|
|53613 |13-1 |Permit |16A |
|53613 |13-2 |Survey |15 |
|53613 |13-3 |Hours |14 |
|1063 |63-1 |Permit |16 |
|1063 |63-2 |Survey |13 |
我知道使用 Where Status LIKE '16%' 將只返回某些版本為 16 的行,而不是與項目相關的其他行。 我還找到了 Exist 的解決方案,但這似乎適用於使用來自 2 個單獨表的數據時。 我確信解決方案很簡單,但我一直無法找到它。 有人可以幫忙嗎?
您可以嘗試將EXISTS
子查詢與LIKE
一起使用
SELECT *
FROM T t1
WHERE EXISTS (
SELECT 1
FROM T tt
WHERE Status LIKE '16%' AND tt.Project = t1.Project
)
您可以使用exists
:
select e.*
from example e
where exists (select 1
from example e2
where e2.Project = e.Project and
e2.Status like '16%'
);
我不確定您所說的“包含”是什么意思。 上面將其解釋為“開始於”的意思。 如果16
可以出現在任何地方,則使用:
e2.Status like '%16%'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.