34,594
社区成员
发帖
与我相关
我的任务
分享
create table test
(
testid int not null,
testname nvarchar(50) null,
testpassword nvarchar(50) null
)
insert into test(testid,testname,testpassword) values(1,'张三','12')
insert into test(testid,testname,testpassword) values(2,'李四','34')
insert into test(testid,testname,testpassword) values(3,'王五','56')
insert into test(testid,testname,testpassword) values(4,'赵六','78')
select testid,testname from test
select testpassword from test
--select testid,testname from test
--select testpassword from test
--这两条语句是必须的,主要是通过这两条语句把两条语句的查询结果拼接成一个table
--想要的结果
testid testname testpassword
----------- -------------------------------------------------- --------------------------------------------------
1 张三 12
2 李四 34
3 王五 56
4 赵六 78
--建表
create table test1
(
testNameA nvarchar(50) ,
testCountA int
)
insert into test1
select '张三','80' union all
select '李四','85' union all
select '王二','90'
create table test2
(
class nvarchar(50) ,
teacher nvarchar(50)
)
insert into test2
select '一班','土豆' union all
select '三班','大米' union all
select '七班','地瓜'
--表信息
select * from test1
--testNameA testCountA
-------------------- -----------
--张三 80
--李四 85
--王二 90
--
--(3 行受影响)
select * from test2
--select * from test1
--class teacher
-------------------- -----------
--一班 土豆
--三班 大米
--七班 地瓜
--
--(3 行受影响)
--处理
;WITH
TB1 AS(select test1.testNameA,test1.testCountA,ROW_NUMBER()OVER(order by getdate())AS num1 from test1),
TB2 AS(select test2.teacher,ROW_NUMBER()OVER(order by getdate())AS num2 from test2)
select TB1.testNameA , TB1.testCountA, TB2.teacher from TB1 LEFT JOIN TB2 ON TB1.num1=TB2.num2
--testNameA testCountA teacher
--------------------------- ----------- ----------------
--张三 80 土豆
--李四 85 大米
--王二 90 地瓜
--
--(3 行受影响)
--删除
drop table test1,test2
select testid,testname,testpassword from test
--testid testname testpassword
------------- --------------- ----------------------
--1 张三 12
--2 李四 34
--3 王五 56
--4 赵六 78
--
--(4 行受影响)
;with test1 as(select testid,testname from test ),
test2 as (select testpassword ,testid from test )
select test1.testid,test1.testname,test2.testpassword from test1
left join test2 on test2.testid=test1.testid
--testid testname testpassword
------------- --------------- ----------------------
--1 张三 12
--2 李四 34
--3 王五 56
--4 赵六 78
--
--(4 行受影响)
楼主你想干什么啊,是不是问错了