select a,b,(select c from t1 t where t.a=t1.a ) from t1 采取用中间表的形式检索数据,用两个表的a关联,但有可能因为返回的c值多于一个而导致查询执行失败. select a,b,c from t1 直接从t1表里面检索数据
select a,b,(select c from t1 t where t.a=t1.a ) from t1 这句是子查询查出一条后,主语句执行一次,然后再执行子查询这样循环执行吗
Create table #1 (a int ,b int, c int) insert into #1 select 1,2,1 insert into #1 select 1,1,2 insert into #1 select 1,3,3 insert into #1 select 1,4,4 select * from #1 select a,b,(select c from #1 t where t.a=#1.a ) from #1 --服务器: 消息 512,级别 16,状态 1,行 2子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。 '正如2楼所言'
不一定一样! 如下当a相同,c列有不同值使就会出错 create table t1(a int,b int,c int) insert into t1 select 1,2,3 union all select 1,2,4 union all select 2,1,3 select a,b,(select c from t1 t where t.a=t1.a ) from t1 /*服务器: 消息 512,级别 16,状态 1,行 1 子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。 */select a,b,c from t1 /* a b c ----------------------- 1 2 3 1 2 4 2 1 3 */drop table t1
[code]--这样执行肯定不会出错: select a,b,(select top 1 c from t1 t where t.a=t1.a ) from t1[/code]
采取用中间表的形式检索数据,用两个表的a关联,但有可能因为返回的c值多于一个而导致查询执行失败.
select a,b,c from t1
直接从t1表里面检索数据
这句是子查询查出一条后,主语句执行一次,然后再执行子查询这样循环执行吗
Create table #1 (a int ,b int, c int)
insert into #1 select 1,2,1
insert into #1 select 1,1,2
insert into #1 select 1,3,3
insert into #1 select 1,4,4
select * from #1
select a,b,(select c from #1 t where t.a=#1.a ) from #1
--服务器: 消息 512,级别 16,状态 1,行 2子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
'正如2楼所言'
不一定一样! 如下当a相同,c列有不同值使就会出错
create table t1(a int,b int,c int)
insert into t1
select 1,2,3 union all
select 1,2,4 union all
select 2,1,3
select a,b,(select c from t1 t where t.a=t1.a ) from t1 /*服务器: 消息 512,级别 16,状态 1,行 1
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
*/select a,b,c from t1 /*
a b c
-----------------------
1 2 3
1 2 4
2 1 3
*/drop table t1
select a,b,(select top 1 c from t1 t where t.a=t1.a ) from t1[/code]