读书人

请问一个SQL语句写法

发布时间: 2012-03-14 12:01:12 作者: rapoo

请教一个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时

读书人网 >SQL Server

热点推荐