34,588
社区成员
发帖
与我相关
我的任务
分享
/*
为了学习sql,强制自己用sql操作表,即使再简单的查询也要用sql测试
作者:Jully
*/
if object_id('t1') is not null
drop table t1
go
if object_id('t2') is not null
drop table t2
go
create table t1
(
[id] int,
num int
)
go
create table t2
(
[id] int,
num int
)
go
insert t1 select 2,4
union all select 3,6
union all select 6,9
union all select 3,8
go
insert t2 select 1,3
union all select 2,5
union all select 4,7
union all select 2,9
go
if object_id('t3') is not null
drop table t3
go
-- select
-- (select [id] from t1 where [id]>3) as id1,
-- (select num from t1 where num<7) as num1,
-- (select [id] from t2 where [id]<5) as id2,
-- (select num from t2 where num>5) as num2
-- into t3 //这是一开始的写法,t3表能生成,且有4个字段 id1,num1,id2,num2 但是语句不成功
--改为如下
select *
into t3
from
(
select [id] as id1,num as num1 from t1 where [id]>3 and num<7
union all
select [id] as id2,num as num2 from t2 where [id]<5 and num>5
) as a
go
select * from t3
/*
但是结果为
(所影响的行数为 2 行)
id1 num1
--- ----
4 7
2 9
-------
*/
create table t1([id] int,num int)
create table t2([id] int,num int)
insert t1 select 2,4
union all select 3,6
union all select 6,9
union all select 3,8
go
insert t2 select 1,3
union all select 2,5
union all select 4,7
union all select 2,9
go
;with c1 as(
select id as id1,row_number()over(order by (select 1))rn from t1 where id>3
),c2 as(
select num as num1,row_number()over(order by (select 1))rn from t1 where num<7
),c3 as(
select id as id2,row_number()over(order by (select 1))rn from t2 where id<5
),c4 as(
select num as num2,row_number()over(order by (select 1))rn from t2 where num>5
),c5 as(
select rn from c1 union select rn from c2 union select rn from c3 union select rn from c4
)select b.id1,c.num1,d.id2,e.num2
from c5 a left join c1 b on a.rn=b.rn
left join c2 c on a.rn=c.rn
left join c3 d on a.rn=d.rn
left join c4 e on a.rn=e.rn
/*
id1 num1 id2 num2
----------- ----------- ----------- -----------
6 4 1 7
NULL 6 2 9
NULL NULL 4 NULL
NULL NULL 2 NULL
(4 行受影响)
*/
go
drop table t1,t2
select [id] as id1 from t1 where [id]>3
id1
--
6
select num as num1 from t1 where num<7
num1
---
4
6
select [id] as id2 from t2 where [id]<5
id2
--
1
2
4
2
select num as num2 from t2 where num>5
num2
----
7
9
/*
最后的结果t3,应为
id1 num1 id2 num2
-- ---- --- ----
6 4 1 7
null 6 2 9
null null 4 null
null null 2 null
*/
create table t1
(
[id] int,
num int
)
go
create table t2
(
[id] int,
num int
)
go
insert t1 select 2,4
union all select 3,6
union all select 6,9
union all select 3,8
go
insert t2 select 1,3
union all select 2,5
union all select 4,7
union all select 2,9
go
if object_id('t3') is not null
drop table t3
go
select a.id,a.num,b.id as id1,b.num as num1
from t1 a ,t2 b
/*
id num id1 num1
----------- ----------- ----------- -----------
2 4 1 3
3 6 1 3
6 9 1 3
3 8 1 3
2 4 2 5
3 6 2 5
6 9 2 5
3 8 2 5
2 4 4 7
3 6 4 7
6 9 4 7
3 8 4 7
2 4 2 9
3 6 2 9
6 9 2 9
3 8 2 9
(16 行受影响)
*/
go
drop table t1,t2
select
a.id,a.num,b.id,b.num
from
(
select px=row_number()over(order by getdate()),[id] as id1,num as num1 from t1 where [id]>3 and num<7)a
join
(
select px=row_number()over(order by getdate()),[id] as id2,num as num2 from t2 where [id]<5 and num>5
)b
on
a.px=b.px
if object_id('t1') is not null
drop table t1
go
if object_id('t2') is not null
drop table t2
go
create table t1
(
[id] int,
num int
)
go
create table t2
(
[id] int,
num int
)
go
insert t1 select 2,4
union all select 3,6
union all select 6,9
union all select 3,8
go
insert t2 select 1,3
union all select 2,5
union all select 4,7
union all select 2,9
go
if object_id('t3') is not null
drop table t3
go
-- select
-- (select [id] from t1 where [id]>3) as id1,
-- (select num from t1 where num<7) as num1,
-- (select [id] from t2 where [id]<5) as id2,
-- (select num from t2 where num>5) as num2
-- into t3 //这是一开始的写法,t3表能生成,且有4个字段 id1,num1,id2,num2 但是语句不成功
--改为如下
select *
into t3
from
(
select * from
(select [id] as id1,num as num1 from t1 where [id]>3 and num<7) a
full join
(select [id] as id2,num as num2 from t2 where [id]<5 and num>5) b
on a.id1=b.id2
) as a
go
select * from t3
/*
id1 num1 id2 num2
----------- ----------- ----------- -----------
NULL NULL 4 7
NULL NULL 2 9
(2 行受影响)