Home > database >  SQL query table more, who is a great god help me how to write a statement
SQL query table more, who is a great god help me how to write a statement

Time:09-27

Table 1
A1 b1 c1
Zhang SAN 15 12
Li si 10 9

Table 2
A1 b1 c1
Zhang SAN 11 14
Just soso 8 6

Use SQL query table 1 and table 2, the first after the output is as follows:

A1 b1 c1 d1 e1
Zhang SAN 12 November 14 15
Li si 10 9 just soso 8 6
The output of the how to write code that

CodePudding user response:

 USE tempdb for 
GO
IF OBJECT_ID (' dbo. (t1) ') IS NOT NULL
DROP TABLE dbo. (t1)
GO
The CREATE TABLE dbo. (t1) (
[a1] NVARCHAR (10)
, b1 NVARCHAR (10)
, [c1] NVARCHAR (10)
)
GO
SET NOCOUNT ON
INSERT INTO dbo. (t1) VALUES (N 'zhang' N '15', N '12')
INSERT INTO dbo. (t1) VALUES (N 'bill', N '10', N '9')
GO
IF OBJECT_ID (' dbo. (t2) ') IS NOT NULL
DROP TABLE dbo. (t2)
GO
The CREATE TABLE dbo. (t2) (
[a1] NVARCHAR (10)
, b1 NVARCHAR (10)
, [c1] NVARCHAR (10)
)
GO
SET NOCOUNT ON
INSERT INTO dbo. (t2) VALUES (N 'zhang' N '11', N '14')
INSERT INTO dbo. (t2) VALUES (N 'just soso' N '8', N '6')
-- -- -- -- -- -- -- -- -- more than for test data -- -- -- -- -- -- -- -- -- --

The SELECT ISNULL (t1. A1, t2. A1) AS a1
B1
, t1., t1. C1
T2. B1 AS d1
, t2. C1 AS e1
The FROM t1 full JOIN t2 ON t1. A1=t2. A1
/*
A1 b1 c1 d1 e1
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Zhang SAN 12 November 14 15
Li si 10 9 NULL NULL
Just soso NULL NULL 8 6
*/

CodePudding user response:

reference 1/f, gypsy song response:
 USE tempdb for 
GO
IF OBJECT_ID (' dbo. (t1) ') IS NOT NULL
DROP TABLE dbo. (t1)
GO
The CREATE TABLE dbo. (t1) (
[a1] NVARCHAR (10)
, b1 NVARCHAR (10)
, [c1] NVARCHAR (10)
)
GO
SET NOCOUNT ON
INSERT INTO dbo. (t1) VALUES (N 'zhang' N '15', N '12')
INSERT INTO dbo. (t1) VALUES (N 'bill', N '10', N '9')
GO
IF OBJECT_ID (' dbo. (t2) ') IS NOT NULL
DROP TABLE dbo. (t2)
GO
The CREATE TABLE dbo. (t2) (
[a1] NVARCHAR (10)
, b1 NVARCHAR (10)
, [c1] NVARCHAR (10)
)
GO
SET NOCOUNT ON
INSERT INTO dbo. (t2) VALUES (N 'zhang' N '11', N '14')
INSERT INTO dbo. (t2) VALUES (N 'just soso' N '8', N '6')
-- -- -- -- -- -- -- -- -- more than for test data -- -- -- -- -- -- -- -- -- --

The SELECT ISNULL (t1. A1, t2. A1) AS a1
B1
, t1., t1. C1
T2. B1 AS d1
, t2. C1 AS e1
The FROM t1 full JOIN t2 ON t1. A1=t2. A1
/*
A1 b1 c1 d1 e1
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Zhang SAN 12 November 14 15
Li si 10 9 NULL NULL
Just soso NULL NULL 8 6
*/
 thank you, try it tomorrow
  • Related