Sql 查询中的多个where条件
我的桌子 第一个问题Sql 查询中的多个where条件,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我的桌子 第一个问题 ID | Name | Type ---+------+----- 1 | asd | A 2 | zxc | B 3 | qwe | A 给予 第二个问题 ID | TypeA_Name ---+----------- 1 | asd 3 | qwe 给予 select ID, TypeB_Name MyTable Where Type = 'B' 我在下面的单个查询a中输入a_名称和B_名称 ID | TypeB_Name ---+-----
ID | Name | Type
---+------+-----
1 | asd | A
2 | zxc | B
3 | qwe | A
给予
第二个问题
ID | TypeA_Name
---+-----------
1 | asd
3 | qwe
给予
select ID, TypeB_Name MyTable Where Type = 'B'
我在下面的单个查询a中输入a_名称和B_名称
ID | TypeB_Name
---+-----------
2 | zxc
应该给
select ID, TypeA_Name, TypeB_Name from MyTable
那么,我如何构建where子句呢?谢谢试试这个:
ID | TypeA_Name | TypeB_Name
---+------------+-----------
1 | asd | ---
2 | --- | zxc
3 | qwe | ---
您可以在正常情况下使用WHERE
s,例如WHERE Type='A'
尝试以下方法:
ID | TypeA_Name | TypeB_Name
---+------------+-----------
1 | asd | ---
2 | --- | zxc
3 | qwe | ---
您可以正常使用WHERE
s,例如WHERE Type='A'
select
ID,
case when Type='A' then Name else null end as TypeA_Name,
case when Type='B' then Name else null end as TypeB_Name
from MyTable