sql 查询问题

表:yd

字段:

bgid pay

1 1

1 1

1 0

2 1

3 0

要求: bgid 值相同时,pay全部为1 才输出

以上输出结果: bgid 2

回复讨论(解决方案)

有人会不,有人会不,

select * from yd where bgid not in (select bgid from yd where pay !=1)

select bgid, pay
from (select *, count(*) as c, sum(pay) as s from yd group by bgid) t
where c=s

select bgid,pay from yd where bgid not in(select bgid from yd where pay=0);

select count(select bgid from yd where pay=1 group by bgid) as result

不知有没有问题?

改正如下:

select bgid,count(*) as result from table_1 where pay=1 group by bgid

统计的是pay=1对应的bgid字段重复出现的次数

Posted in 未分类

发表评论