关于sql的执行顺序有关问题
关于sql的执行顺序问题
这是表
use tempdb;
go
if OBJECT_ID('dbo.t1') is not null
drop table dbo.t1
go
create table dbo.t1(col1 int not null primary key);
insert into dbo.t1(col1) values(1);
insert into dbo.t1(col1) values(2);
insert into dbo.t1(col1) values(3);
insert into dbo.t1(col1) values(100);
insert into dbo.t1(col1) values(101);
insert into dbo.t1(col1) values(103);
insert into dbo.t1(col1) values(104);
insert into dbo.t1(col1) values(105);
insert into dbo.t1(col1) values(106);
这是t-sql语句
--table 4-43 分组因子
select col1,
(select min(col1) from dbo.t1 as b where b.col1>=a.col1
and not exists (select col1 from dbo.t1 as c where b.col1+1=c.col1)) as grp
from dbo.t1 as a
该语句是从最内层的 子查询c表执行,但是where条件里面有b表所以再向外查询外层语句到b表再到a表?
还是先从 from a表开始,然后执行select部分再处理 选取列包含的子查询?
------解决方案--------------------
它是SELECT中的子查询的子查询部分,只跟B有关,而这个的外层,也就是SELECT中的子查询才跟A有关
这是表
use tempdb;
go
if OBJECT_ID('dbo.t1') is not null
drop table dbo.t1
go
create table dbo.t1(col1 int not null primary key);
insert into dbo.t1(col1) values(1);
insert into dbo.t1(col1) values(2);
insert into dbo.t1(col1) values(3);
insert into dbo.t1(col1) values(100);
insert into dbo.t1(col1) values(101);
insert into dbo.t1(col1) values(103);
insert into dbo.t1(col1) values(104);
insert into dbo.t1(col1) values(105);
insert into dbo.t1(col1) values(106);
这是t-sql语句
--table 4-43 分组因子
select col1,
(select min(col1) from dbo.t1 as b where b.col1>=a.col1
and not exists (select col1 from dbo.t1 as c where b.col1+1=c.col1)) as grp
from dbo.t1 as a
该语句是从最内层的 子查询c表执行,但是where条件里面有b表所以再向外查询外层语句到b表再到a表?
还是先从 from a表开始,然后执行select部分再处理 选取列包含的子查询?
------解决方案--------------------
它是SELECT中的子查询的子查询部分,只跟B有关,而这个的外层,也就是SELECT中的子查询才跟A有关