日期:2014-05-19  浏览次数:20880 次

请教大侠们,这样的查询能实现吗???????????????
表一
A列 A列
0001 1
0001 2
0001 3
0001 4
0001 5
0001 6
0001 7
0001 8
0001 9
0001 10
0001 11
0001 12
0002 10
0003 1
0003 2
0003 3
0003 4
0003 5
0003 6
0003 7
0003 8
0003 9
0003 10
0003 11
0003 12
---------
想查询出上表中A列值 '0002 '和B列值 '10 '这一条记录,如何查询,谢谢!!!




------解决方案--------------------
select
t.*
from
表一 t
where
not exists(select 1 from 表一 where A列=t.A列 and B列!=t.B列)
------解决方案--------------------
declare @t table(A varchar(4),B int)
insert into @t select '0001 ',1
insert into @t select '0001 ',2
insert into @t select '0001 ',3
insert into @t select '0001 ',4
insert into @t select '0001 ',5
insert into @t select '0001 ',6
insert into @t select '0001 ',7
insert into @t select '0001 ',8
insert into @t select '0001 ',9
insert into @t select '0001 ',10
insert into @t select '0001 ',11
insert into @t select '0001 ',12
insert into @t select '0002 ',10
insert into @t select '0003 ',1
insert into @t select '0003 ',2
insert into @t select '0003 ',3
insert into @t select '0003 ',4
insert into @t select '0003 ',5
insert into @t select '0003 ',6
insert into @t select '0003 ',7
insert into @t select '0003 ',8
insert into @t select '0003 ',9
insert into @t select '0003 ',10
insert into @t select '0003 ',11
insert into @t select '0003 ',12

select t.* from @t t where not exists(select 1 from @t where A=t.A and B!=t.B)

/*
A B
---- -----------
0002 10
*/
------解决方案--------------------
还是这样?
select A列,min(B列) from t group by A列 having count(*) = 1
------解决方案--------------------
如果B列没有记录数时,只有通过临时表实现

select * ,id=identity(1,1) into #
from 表
update b
set id=(select count(*)from # where A列=b.A列 and id!> b.id)--生成记录数
from # b
查询
select * from # where A列 = '0002 ' and id = '10 '
------解决方案--------------------
这样?
select * from t where A列 = '0002 ' and B列 <> '12 '
------解决方案--------------------
--试试
update t _a
set B列 = 12
where exists(
select 1
from t
group by A列
having count(*) = 1
and min(B列) <> '12 '
and _a.A列 = A列
and _a.B列 = min(B列)
)

------解决方案--------------------
郁闷在我发前问题已经解决了

那就揭贴吧!~