日期:2014-05-18  浏览次数:20630 次

请教一个SQL语句写法。
一个表中有一个bit   类型的段,和一个ItemID(可以重复),现在根据ItemId返回多条记录,要检测记录当其中所有bit型的字段都为真时,   as   一个字段为真,不用遍历,有什么语句或方法?

------解决方案--------------------

select * from ta a
where ItemID=....
and
not exists(select 1 from ta where ItemID=a.ItemID and [bit]=0 )

------解决方案--------------------
ItemID=....--指定条件(要取的ItemID)

不指定时,把所有满足的都选出来
select * from ta a
where
not exists(select 1 from ta where ItemID=a.ItemID and [bit]=0 )
------解决方案--------------------
select (case when count(a)=sum(a) then 1 else 0) as '一个字段为真 '
from 表 where idtmid=条件
没考虑bit为null时