Home  >  Article  >  Database  >  mssql 两表合并sql语句

mssql 两表合并sql语句

WBOY
WBOYOriginal
2016-06-07 17:58:351075browse

mssql 两表合并sql语句,下面是简单的例子,大家可以参考下。

学生表: 课程表:

id 姓名 课程号(外键) 课程号,课程名

'1', 'xix', 1 1,' 语文'
'2', 'cic', 2 2, '数学'
'3', 'ddi', 4 3, '英语'

将学生表、课程表合成一个新表 destTb:

id 姓名 课程号 课程名

1 xix 1 语文
2 cic 2 数学
3 ddi NULL NULL
NULL NULL 3 英语

CREATE TABLE student(id nvarchar(10),name nvarchar(10),cno int)
INSERT student SELECT '1','xix',1
UNION ALL SELECT '2','cic',2
UNION ALL SELECT '3','ddi',4
GO

CREATE TABLE class(cno int,name nvarchar(10))
INSERT class SELECT 1,'语文'
UNION ALL SELECT 2,'数学'
UNION ALL SELECT 3,'英语'
GO

select id ,s.name as 姓名,c.cno as cno,c.name as 课程 FROM student as sclass as c s.cno=c.cno

-- ,结果集作为tmp

( as tmp

--如果目标表

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn