SQL 查询语句,一个表中 两个字段,一个相同另一个不同

例如表中有两列字段A 和 B
B字段只有两种状态:True 和 False

我想查询出 字段A的数据即 符合True,也符合False的记录数。

例如:
A B
1 True
1 false
2 True
3 True
3 True
4 False
4 false

结果为 1个
最新回答
年華不似水

2024-05-06 00:17:04

SELECT count(*)
FROM [SELECT 表1.A, sum(表1.B) AS BB
FROM 表1
GROUP BY 表1.A]. AS [last]
WHERE BB=-1;

'因为True+false=-1 ,以上语句在access2003中通过。结果为1
'只是不知道1个A 会不会有很多B状态
比如
1 true
1 true
1 false
1 false

这样的话就不行了。另外提问者没有交待清楚B字段是何类型,我理解应该是Boolean型的吧。可是那个Flase一会大写,一会小写,会不会是文本型呢?提问者要告诉我们是在哪个系统中用的。不同的系统略有差异啊。
喻夜白

2024-05-06 01:27:59

应该很简单呀
select count(distinct(t1.A))
from table t1, table t2
where t1.A=t2.A and t1.B !=t2.B
美女天下

2024-05-06 04:40:17

SELECT COUNT1+COUNT2 FROM
(select A,count(*) COUNT1 from table_name where B='True' GROUP BY A) C,
(select A,count(*) COUNT2 from table_name where B='False' OR B='false' GROUP BY A) D
where C.A=D.A
薄荷凉我心

2024-05-06 16:42:00

1楼的回答跟我类似不过纠正下:
select distinct A from mytable where A in
(
select A from mytable where B='true '
) and A in
(
select A from mytable where B='false '
)
软耳兔兔

2024-05-06 16:29:19

select a, sum(T) as T, sum(F) as F
(select a, case b = 'True' then 1 else 0 end as T,
case b <> 'True' then 1 else 0 end as F
from 表)
group by a
having sum(T) > 0 and sum(F) > 0