Sql数据库查询,如何实现只显示为某值的字段?

例如字段A='yes',B='yes',C='no'
希望查询结果只显示字段A,B,不显示C。
开始以为用'or'可以,select * from table where A='yes' or B='yes' or C='yes'
但这样只要A,B,C中有一个是yes,就会显示全部
还有其他办法吗?

  实现只显示为某值的字段,可以通过行列转换实现。
  以下是以sql server为例来说明:
  select b.stu_name,
  max(case a.subject when '语文' then a.grade else '' end) as 语文,
  max(case a.subject when '数学' then a.grade else '' end) as 数学,
  max(case a.subject when '英语' then a.grade else '' end) as 英语
  from stu_grade a,stu_master b
  where a.stu_no=b.stu_no
  group by b.stu_name
  数据库为oralce的话执行
  select b.stu_name,
  max(case a.subject when '语文' then to_char(a.grade) else '' end) as 语文,
  max(case a.subject when '数学' then to_char(a.grade) else '' end) as 数学,
  max(case a.subject when '英语' then to_char(a.grade) else '' end) as 英语
  from stu_grade a,stu_master b
  where a.stu_no=b.stu_no
  group by b.stu_name
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-12-16
你这个表应该是个横式,所以应该先转换成直式
我给你举个例子吧
先建立一个表
CREATE TABLE YesNo
(Column1 char(10),
Column2 char(10),
Column3 char(10),)
GO
/*插入数据*/
INSERT YesNo
VALUES('Yes','Yes','No')
/*你应该说的就是这个意思吧*/
然后执行下列语句将横式转化成直式
SELECT *
FROM YesNo
UNPIVOT (YesNo FOR 列 IN (Column1,Column2,Column3))AS My_unpivot
这样就很清晰的看出自己的哪个列是YES 哪个列是NO
然后在执行
SELECT 列
FROM YesNo
UNPIVOT (YesNo FOR 列 IN (Column1,Column2,Column3))AS My_unpivot
WHERE YesNo = 'Yes'
就可以查到了 你可以自己执行以下 自己理解以下
PS:我要出门了 下午才能回来 不懂的可以问本回答被网友采纳
第2个回答  2020-02-15
首先,要确认该字段是否允许为空,如果不允许则无法插入。
如果该字段允许为空则可参考以下方法插入:
比如数据表table的字段有:name,email,addr。其中addr可以为空,并插入空值。sql语句如下:
insert into table(name,email) values('xiaoming','my email')
第3个回答  2012-05-10
用case when 先来判断出谁是YES or NO 的字段 给别名 再通过子查询来获取你要的数据显示出来。
第4个回答  2012-05-10
问题不可实现吧;
如查询字段
A='yes',B='yes',C='no' 为 A,B
A='yes',B='no',C='no' 为 A
A='yes',B='yes',C='yes' 为 A,B,C
要显示字段列数明显不同(一个查询应该不可实现)
相似回答