如何只从sqlserver中选择真值

时间:2016-07-17 08:54:56

标签: sql sql-server sql-server-2008

如何从sqlserver中仅选择真值?我写了一个查询,但是当我在sqlserver中测试这个查询时,返回所有字段而不是真实字段。

SELECT Fruit,Drink,Desert,MainFood,Salad,TableFlower,SaloonLighting,
Saloondesign,SloonCrew,Pastry,GiftCard 
FROM WeedingSalonGeneralRes 
WHERE Fruit='True' and Drink='True'and Desert='True'and MainFood='True'and 
Salad='True'and TableFlower='True'and SaloonLighting='True'and 
Saloondesign='True'and SloonCrew='True'and Pastry='True' and GiftCard='True'

2 个答案:

答案 0 :(得分:0)

如果你的字段类型是BIT,你可以这样做:

SELECT Fruit,Drink,Desert,MainFood,Salad,TableFlower,SaloonLighting,
Saloondesign,SloonCrew,Pastry,GiftCard 
FROM WeedingSalonGeneralRes 
WHERE Fruit=1 and Drink=1 and Desert=1 and MainFood=1 and 
Salad=1 and TableFlower=1 and SaloonLighting=1 and 
Saloondesign=1 and SloonCrew=1 and Pastry=1 and GiftCard=1

否则你能解释一下你想要的输出吗?

答案 1 :(得分:0)

在同一行你不能。

但你可以做到:

   with distinctvalueyes (typearticle) as (
   select top 1 'Fruit' FROM WeedingSalonGeneralRes where Fruit=1
   union all 
   select top 1 'Drink' FROM WeedingSalonGeneralRes where Drink=1
   union all 
   select top 1 'Desert' FROM WeedingSalonGeneralRes where Desert=1
   union all 
   select top 1 'MainFood' FROM WeedingSalonGeneralRes where MainFood=1
   union all 
   select top 1 'Salad' FROM WeedingSalonGeneralRes where Salad=1
   union all 
   select top 1 'TableFlower' FROM WeedingSalonGeneralRes where TableFlower=1
   union all 
   select top 1 'SaloonLighting' FROM WeedingSalonGeneralRes where SaloonLighting=1
   union all 
   select top 1 'Saloondesign' FROM WeedingSalonGeneralRes where Saloondesign=1
   union all 
   select top 1 'SloonCrew' FROM WeedingSalonGeneralRes where SloonCrew=1
   union all 
   select top 1 'Pastry' FROM WeedingSalonGeneralRes where Pastry=1
   union all 
   select top 1 'GiftCard' FROM WeedingSalonGeneralRes where GiftCard=1
   )
   select * from distinctvalueyes