当前位置:首页 » 编程语言 » sql表合并
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql表合并

发布时间: 2022-02-21 16:44:55

A. 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



*/


B. SQL语句两个表合并合计

select名称=a.名称,A数量=a.数量,A金额=a.金额,B数量=b.数量,B金额=b.金额
from(select名称,数量=sum(数量),金额=sum(金额)fromA表groupby名称)a
fulljoin(select名称,数量=sum(数量),金额=sum(金额)fromB表groupby名称)b
ona.名称=b.名称

C. 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]

请采纳!

D. 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)

E. sql怎么把两张表合并成一张

sql把两张表合并成一张步骤如下:

1、我们首先将以A为基准用leftjoin以B为基准用right,都要就用fulljoin 。

F. 用SQL语言将两张表合并起来

用经销商代码做为key,连接两张表格。

select * from 表1,表2 where 表1.经销商代码=表2.经销商代码

G. 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;

H. SQL 表如何两张表合并成一张 !!

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

SQL UNION 语法

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

注释:默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。
SQL UNION ALL 语法

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

另外,UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

I. SQL怎样合并两个表进行查询

selecttop10*from
(select*from表1
unionall
select*from表2)asT
orderby某字段

union all就是把俩表连接

然后把他俩连接的结果集起名叫T

然后你就可以用top了

你可以直接运行一下括号里那句,如果表结构相同你两个可以用*,如果部分字段相同,你就要把在一起的字段写到一起了,比如

selectid,namefrom表1
unionall
selectid,namefrom表2

类似这样

J. sql 两表合并汇总

我想楼主说的应该是这个意思:
select
字段1,字段2,sum(起初数量),sum(发生数据)
from
(
select
字段1,
字段2,
sum(起初数量)
as
起初数量,
0
as
发生数据
from
table1
group
by
字段1,字段2
union
select
字段1,
字段2,
0
as
起初数量,
sum(发生数据)
as
发生数据
from
table2
group
by
字段1,字段2
)
a
group
by
字段1,字段2
因为表里面不可能就一两条数据-_-