求个简单的SQL语句

bolahbc 2011-01-10 10:32:44

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
...全文
77 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bolahbc 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 joyhen 的回复:]
这里需要陪一个关联的列:

SQL code
--建表
create table test1
(
testNameA nvarchar(50) ,
testCountA int
)
insert into test1
select '张三','80' union all
select '李四','85' union all
select '王二','90'……
[/Quote]

我用的sql2000 没有row_number() 但是这种也可以实现 学习了
thanks
bolahbc 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dawugui 的回复:]
引用 6 楼 bolahbc 的回复:
不存在表和表之间的关系

那就比较麻烦了,如下两种方法,自己选择一个.
--1.假设每个查询都能根据某(些)字段区分出大小.使用排名,然后union all
--sql 2000 (使用子查询获取排名)
select m.* , n.* from
(select t.* , px = (select count(1) from tb1 wher……
[/Quote]

明白了 thanks
结贴
joyhen 2011-01-10
  • 打赏
  • 举报
回复
这里需要陪一个关联的列:
--建表
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


dawugui 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 bolahbc 的回复:]
不存在表和表之间的关系
[/Quote]
那就比较麻烦了,如下两种方法,自己选择一个.
--1.假设每个查询都能根据某(些)字段区分出大小.使用排名,然后union all
--sql 2000 (使用子查询获取排名)
select m.* , n.* from
(select t.* , px = (select count(1) from tb1 where id < t.id) + 1 from tb1 t) m
full join
(select t.* , px = (select count(1) from tb2 where id < t.id) + 1 from tb2 t) n
on m.px = n.px

--sql 2005 (使用row_number获取排名)
select m.* , n.* from
(select t.* , px = row_number() over(order by id) from tb1 t) m
full join
(select t.* , px = row_number() over(order by id) from tb2 t) n
on m.px = n.px

--2,如果不存在能达到1的可能性,就需要使用临时表.
select t.* , id = identity(int,1,1) into tmp1 from tb1 t
select t.* , id = identity(int,1,1) into tmp2 from tb2 t

select m.* , n.* from tmp1 m full join tmp2 on m.id = n.id
bolahbc 2011-01-10
  • 打赏
  • 举报
回复
不存在表和表之间的关系
bolahbc 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dawugui 的回复:]
看不懂问题?

你直接一个语句不就OK了?
[/Quote]
=。=好吧,我承认我把简单的问题复杂化了

我主要想知道如何通过这2条语句把结果合并起来

比如我查询的是2张表 查询出的行数都一样

select ID,columns1 from test1
select columns2,columns3 from test2
通过2条语句的组合得出下列结果
ID columns1 columns2 columns3
1 x1 x2 x3

我的表达能力使我太悲剧了。。

joyhen 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xuam 的回复:]
悲剧了!
[/Quote]

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 行受影响)
楼主你想干什么啊,是不是问错了
rucypli 2011-01-10
  • 打赏
  • 举报
回复
select * from test这个不是你要的结果吗
dawugui 2011-01-10
  • 打赏
  • 举报
回复
看不懂问题?

你直接一个语句不就OK了?
xuam 2011-01-10
  • 打赏
  • 举报
回复
悲剧了!

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧