当前位置: 代码迷 >> Sql Server >> [帖]使用SQL代码来完成两个数据表的整合
  详细解决方案

[帖]使用SQL代码来完成两个数据表的整合

热度:467   发布时间:2016-04-27 12:39:56.0
[求助帖]使用SQL代码来完成两个数据表的整合
环境:
SQL SERVER 2000
WIN SERVER 2003

数据库:db1

表TB1
stu_id stu_class stu_name
1001 2 小张
1002 3 小王
1003 7 小赵


表TB2

stu_grade stu_id stu_mobile
1 1001 13712345678
3 1002 13798765432
2 1003 13365465891  

欲通过SQL语句完成至以下效果:

stu_id stu_grade stu_class stu_name stu_mobile
1001 1 2 小张 13712345678
1002 3 3 小王 13798765432
1003 2 7 小赵 13365465891



求高手帮忙写段代码,万分感谢!


------解决方案--------------------
SQL code
if object_id('[TB1]') is not null drop table [TB1]gocreate table [TB1] (stu_id int,stu_class int,stu_name nvarchar(4))insert into [TB1]select 1001,2,'小张' union allselect 1002,3,'小王' union allselect 1003,7,'小赵'--> Title  : Generating test data [TB2]--> Author : --> Date   : 2011-10-20 08:10:10if object_id('[TB2]') is not null drop table [TB2]gocreate table [TB2] (stu_grade int,stu_id int,stu_mobile bigint)insert into [TB2]select 1,1001,13712345678 union allselect 3,1002,13798765432 union allselect 2,1003,13365465891select * from [TB1]select * from [TB2]SELECT TB2.stu_id,TB2.stu_grade,TB1.stu_class,TB1.stu_name,TB2.stu_mobileFROM TB2INNER JOIN TB1 ON TB2.stu_id = TB1.stu_id/*stu_id      stu_grade   stu_class   stu_name stu_mobile----------- ----------- ----------- -------- --------------------1001        1           2           小张       137123456781002        3           3           小王       137987654321003        2           7           小赵       13365465891(3 行受影响)*/
  相关解决方案