簡體   English   中英

如何在列中選擇值A或選擇A和B而不選擇B?

[英]How to select value A or both A and B but not B in a column?

我有一張桌子

column1     column2
name1       A
name1       B

在column1中, name1在column2中可以有2個值: AB。

我想這樣選擇:

select *
from tableA
where ??

條件是,當column2等於A 等於A和B,但不等於B 這可能嗎?

我期望在查詢條件下:

column1     column2
name1       A

要么

column1     column2
name1       A
name1       B

我不想要的:

column1     column2
name1       B

您的規格是:

select t.*
from t
where t.column2 = 'A' or
      exists (select 1 from t t2 where t2.column1 = t.column1 and t2.column2 = 'A');

實際上,可以將其簡化為:

select t.*
from t
where exists (select 1 from t t2 where t2.column1 = t.column1 and t2.column2 = 'A');

如果只需要具有此屬性的名稱,則:

select distinct t.column
from t
where t.column1 = 'A';

您可以使用exists

select *
  from tableA a
 where exists ( select 1 from tableA where column1 = a.column1 and column2 = 'A' );

對於當前情況,即使不添加column1 = a.column1 and part,也可以獲得所需的結果。

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM