sql合并表
⑴ sql 合并三个表
那只能将字段功能一样的合并哦! 其它的放空,如下sql脚本
select[列1],[列2],[列3],[列4],[列5],[列6]from[表1]
unionall
select[列1],[列2],[列3],null,null,nullfrom[表2]
unionall
select[列1],[列2],null,null,null,[列6]from[表3]
请采纳!
⑵ 在SQL中,合并多个表中的数据有哪3种方法
有左连left 右连 right 和内敛 innerunion 是纵向显示两个表记录还可以用笛卡尔积
应该是ABC
⑶ sql怎么把两张表合并成一张
sql把两张表合并成一张步骤如下:
1、我们首先将以A为基准用leftjoin以B为基准用right,都要就用fulljoin 。
⑷ 如何用sql语句合并三张表
select a.id, a.data1, a.notes1, b.data2, b.note2, c.data3, c.notes3
into x
from a,b,c
where a.id = b.id and a.id = c.id
这样三张表的数据就都放在表x里了
注意是在表x不存在的情况下
存在的话这样
insert into x
select a.id, a.data1, a.notes1, b.data2, b.note2, c.data3, c.notes3
from a,b,c
where a.id = b.id and a.id = c.id
⑸ SQL怎么合并表
select * into 新表名 from (select * from T1 union all select * from T2)
这个语句可以实现将合并的数据追加到一个新表中。
不合并重复数据 select * from T1 union all select * from T2
合并重复数据 select * from T1 union select * from T2
两个表,表1 表2
如果要将 表1的数据并入表2用以下语句即可
insert into 表2(字段1,字段2) select 字段1,字段2 from b1
注意,必须把字段名全部写清楚,而且不允许把自动编号进去写进去,要合并自动编号字段必须重写一个算法一条一条记录地加进去
1 insert into b1 select * from b2
2 select * into newtable from (select * from b1 union all select * from b2)
⑹ SQL 合并两表
两个表连接查询然后用distinct去掉重复的即可。
给你点思路,先把两个表连接结果作为一张表,然后再从这个表中过滤掉重复的。
select
distinct
t3.编号,t3.名称
from
(
select
t1.编号,t1.名称
from
t1
join
t2
)
as
t3
⑺ SQL求两张表如何合并。
先用左连接将1表完全查出,再用右连接将2表完全查出,最后合并结果
SELECT t1.id AS `编号`,t1.`name` AS `名称`,t1.score AS `科目一分数`,CASE WHEN t2.score IS NULL THEN 0 ELSE t2.score END AS `科目二分数`
FROM t1 LEFT JOIN t2 ON t1.id= t2.id
UNION
SELECT t2.id AS `编号`,t2.`name` AS `名称`,CASE WHEN t1.score IS NULL THEN 0 ELSE t1.score END AS `科目一分数`,t2.score AS `科目二分数`
FROM t1 RIGHT JOIN t2 ON t1.id= t2.id;
⑻ sql如何将多个表合并成一个表
--复杂点的方法,
DECLARE @startTime DATETIME --开始的时间
DECLARE @endTime DATETIME --结束的时间
--比如我要得到1月到2月的数据
SET @startTime = '2014-01-01 00:00:00.000' --从新年第一天开始
SET @endTime = GETDATE() --一直到当前时间
--准备创建表
--根据月份创建
DECLARE @mi INT
--(这里必须要是开始时间和结束时间在一年之内的情况,如果不是在一年以内则要另外考虑,懒得写)
SET @mi = MONTH(@endTime) - MONTH(@startTime) + 1 --用结束月份减去开始月份,得到总共要创建几个月份表(加上当前月份)
--PRINT @mi
WHILE @mi >0
BEGIN
DECLARE @tableName VARCHAR(6) --月份表名
DECLARE @sql NVARCHAR(1000)
SET @tableName = '2014'
IF @mi<10
BEGIN
SET @tableName = @tableName + '0'
END
SET @tableName = @tableName + CAST(@mi AS VARCHAR)
IF NOT EXISTS (SELECT * FROM sysobjects WHERE [name] = @tableName AND [type] = 'U')
BEGIN
--如果当前月份表不存在,创建当前月份表
SET @sql = 'CREATE TABLE '+@tableName + ' (MId INT PRIMARY KEY IDENTITY(1,1))'
PRINT @sql
--执行
--EXECUTE sp_executesql N@sql
END
DECLARE @dayName VARCHAR(8) --天表名
WHILE CONVERT(VARCHAR(6),@endTime,112)=@tableName --确定当前时间是否是当前月的
BEGIN
SET @sql = ''
SET @dayName = CONVERT(VARCHAR(8),@endTime,112)
SET @sql = 'INSERT INTO '+@tableName +' VALUES SELECT * FROM ' + @dayName
PRINT @sql
--执行
--EXECUTE sp_executesql N@sql
SET @sql = ''
SET @endTime = DATEADD(DAY,-1,@endTime)
END
SET @mi = @mi - 1 --月份减一
/*
------------------------------------------------------------
CREATE TABLE 201402 (MId INT PRIMARY KEY IDENTITY(1,1))
INSERT INTO 201402 VALUES SELECT * FROM 20140219
INSERT INTO 201402 VALUES SELECT * FROM 20140218
INSERT INTO 201402 VALUES SELECT * FROM 20140217
INSERT INTO 201402 VALUES SELECT * FROM 20140216
INSERT INTO 201402 VALUES SELECT * FROM 20140215
INSERT INTO 201402 VALUES SELECT * FROM 20140214
INSERT INTO 201402 VALUES SELECT * FROM 20140213
INSERT INTO 201402 VALUES SELECT * FROM 20140212
INSERT INTO 201402 VALUES SELECT * FROM 20140211
INSERT INTO 201402 VALUES SELECT * FROM 20140210
INSERT INTO 201402 VALUES SELECT * FROM 20140209
INSERT INTO 201402 VALUES SELECT * FROM 20140208
INSERT INTO 201402 VALUES SELECT * FROM 20140207
INSERT INTO 201402 VALUES SELECT * FROM 20140206
INSERT INTO 201402 VALUES SELECT * FROM 20140205
INSERT INTO 201402 VALUES SELECT * FROM 20140204
INSERT INTO 201402 VALUES SELECT * FROM 20140203
INSERT INTO 201402 VALUES SELECT * FROM 20140202
INSERT INTO 201402 VALUES SELECT * FROM 20140201
CREATE TABLE 201401 (MId INT PRIMARY KEY IDENTITY(1,1))
INSERT INTO 201401 VALUES SELECT * FROM 20140131
INSERT INTO 201401 VALUES SELECT * FROM 20140130
INSERT INTO 201401 VALUES SELECT * FROM 20140129
INSERT INTO 201401 VALUES SELECT * FROM 20140128
INSERT INTO 201401 VALUES SELECT * FROM 20140127
INSERT INTO 201401 VALUES SELECT * FROM 20140126
INSERT INTO 201401 VALUES SELECT * FROM 20140125
INSERT INTO 201401 VALUES SELECT * FROM 20140124
INSERT INTO 201401 VALUES SELECT * FROM 20140123
INSERT INTO 201401 VALUES SELECT * FROM 20140122
INSERT INTO 201401 VALUES SELECT * FROM 20140121
INSERT INTO 201401 VALUES SELECT * FROM 20140120
INSERT INTO 201401 VALUES SELECT * FROM 20140119
INSERT INTO 201401 VALUES SELECT * FROM 20140118
INSERT INTO 201401 VALUES SELECT * FROM 20140117
INSERT INTO 201401 VALUES SELECT * FROM 20140116
INSERT INTO 201401 VALUES SELECT * FROM 20140115
INSERT INTO 201401 VALUES SELECT * FROM 20140114
INSERT INTO 201401 VALUES SELECT * FROM 20140113
INSERT INTO 201401 VALUES SELECT * FROM 20140112
INSERT INTO 201401 VALUES SELECT * FROM 20140111
INSERT INTO 201401 VALUES SELECT * FROM 20140110
INSERT INTO 201401 VALUES SELECT * FROM 20140109
INSERT INTO 201401 VALUES SELECT * FROM 20140108
INSERT INTO 201401 VALUES SELECT * FROM 20140107
INSERT INTO 201401 VALUES SELECT * FROM 20140106
INSERT INTO 201401 VALUES SELECT * FROM 20140105
INSERT INTO 201401 VALUES SELECT * FROM 20140104
INSERT INTO 201401 VALUES SELECT * FROM 20140103
INSERT INTO 201401 VALUES SELECT * FROM 20140102
INSERT INTO 201401 VALUES SELECT * FROM 20140101
*/
⑼ sql 快速合并2个表
2个sql:
insertintoA(id,col1,col2)
selectid,col1,col2
fromb
wherenotexists(select1fromAwhereA.id=B.id)
更新(sqlserver)
updateAsetA.col1=B.col1,A.col2=B.col2
fromB
whereA.id=B.id
更新(Oracle)
updateAset(col1,col2)=
(selectcol1,col2fromBwhereA.id=B.id
)
andexists(select1fromAwhereA.id=B.id)
⑽ SQL怎样合并两个表进行查询
selecttop10*from
(select*from表1
unionall
select*from表2)asT
orderby某字段
union all就是把俩表连接
然后把他俩连接的结果集起名叫T
然后你就可以用top了
你可以直接运行一下括号里那句,如果表结构相同你两个可以用*,如果部分字段相同,你就要把在一起的字段写到一起了,比如
selectid,namefrom表1
unionall
selectid,namefrom表2
类似这样