求助:一条sql语句

[复制链接]
查看11 | 回复3 | 2015-3-6 11:57:31 | 显示全部楼层 |阅读模式
================
A表
================
a_id ___name
1_______ 李
2_______ 王
3_______ 赵

================
B表
================
c _id____ a_id_____is_ok
1_________1_______ 成功过
2_________1_______ 失败过
3_________2_______ 成功过
A表的字段a_id 与B表的a_id 字段有关联,我想根据A表中的a_id 字段查出B表中的只“成功过”或 只“失败过”的记录
这条语句怎么写啊?
比如,A表中的1这条记录,在B表中它既"成功过"又"失败过",那么1这条记录不是我想要的
而A表中的2这条记录它在B表中只"失败过",我就是想查像2这样的记录
请问sql语句怎么写啊?
回复

使用道具 举报

千问 | 2015-3-6 11:57:31 | 显示全部楼层
select max(c_id),a_id,max(is_ok) ct from b where a_id in(select a_id from a) group by a_id having count(0)<2
回复

使用道具 举报

千问 | 2015-3-6 11:57:31 | 显示全部楼层
select * from a
where a.id in
(
select a_id from
(
select distinct a_id,is_ok from b
)
group by a_id
having count(is_ok) < 2
)
回复

使用道具 举报

千问 | 2015-3-6 11:57:31 | 显示全部楼层
楼上兄弟的语句在 having count(is_ok) < 2改为having count( distinct is_ok) =1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行