日期:2014-05-16  浏览次数:20408 次

MS SQLSERVER问题
我现在有两张表:
student(stuID,stuName)
origin(oriID,oriName)

student中有数据:
stuID stuName
1         张三
2        李四

origin中有数据:
oriID   oriName
1           本地
2           外地

我现在想生成一张表
stuID  stuName  oriID  oriName
1         张三            1          本地
1         张三            2           外地
2        李四             1          本地
2        李四             2          外地

这种如果实现?
------解决方案--------------------
--> 测试数据: [student]
if object_id('[student]') is not null drop table [student]
create table [student] (stuID int,stuName varchar(4))
insert into [student]
select 1,'张三' union all
select 2,'李四'
--> 测试数据: [origin]
if object_id('[origin]') is not null drop table [origin]
create table [origin] (oriID int,oriName varchar(4))
insert into [origin]
select 1,'本地' union all
select 2,'外地'

select * from [student]
select * from [origin]


SELECT A.stuid,A.stuname,b.oriID,b.oriname
FROM [student] A
LEFT JOIN [origin] B ON 1=1

/*
stuid stuname oriID oriname
1 张三 1 本地
1 张三 2 外地
2 李四 1 本地
2 李四 2 外地*/

------解决方案--------------------
--> 测试数据: [student]
if object_id('[student]') is not null drop table [student]
create table [student] (stuID int,stuName varchar(4))
insert into [student]
select 1,'张三' union all
select 2,'李四'
--> 测试数据: [origin]
if object_id('[origin]') is not null drop table [origin]
create table [origin] (oriID int,oriName varchar(4))
insert into [origin]
select 1,'本地' union all
select 2,'外地'
 
select * from [student]
select * from [origin]
 
 



SELECT A.stuid,A.stuname,b.oriID,b.oriname
FROM [student] A CROSS JOIN  [origin] B