怎样把mysql的多张表汇总成一张表_sql如何将多个表合并成一个表

展开全部

--复杂点的方法,

DECLARE @startTime DATETIME--开始e69da5e887aa62616964757a686964616f31333335306262的时间

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 --月份减一

END

/*

------------------------------------------------------------

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

*/

你可能感兴趣的:(怎样把mysql的多张表汇总成一张表_sql如何将多个表合并成一个表)