當前位置:首頁 » 編程語言 » 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
因為表裡面不可能就一兩條數據-_-