Home > database >  Advice on how to query two values in the table!
Advice on how to query two values in the table!

Time:10-17

I want to check two tables
T1
The name of the value of 1
A 10
20 b
30 c
40 d

T2
The name value 2
A 20
C 100
D 50

Want to the results of a query is
The name value 1 2
A 10 20
B 20 0 or null
C 30 100
D 40 to 50



Could you tell me how to write you a great god the?

CodePudding user response:

The test data
 -If not object_id (N 'Tempdb for.. # T1 ') is null 
# drop table T1
Go
# the Create table T1 ([name] nvarchar (21), [1] int)
Insert # T1
Union all select N 'a', 10
20 union all select N 'b',
30 union all select N 'c',
Select N 'd', 40
GO
If not object_id (N 'Tempdb for.. # T2 ') is null
Drop table # T2
Go
The Create table # T2 ([name] nvarchar (21), int value [2])
Insert # T2
20 union all select N 'a',
Select N 'c', 100 union all
Select N 'd', 50
Go
- the end of the test data
SELECT # T1. The name,
Value of 1,
Value 2
The FROM # T1
LEFT the JOIN # T2
ON # T2. Name=# T1. Name;

CodePudding user response:

The
reference 1/f, February 16 response:
 
- test dataIf not object_id (N 'Tempdb for.. # T1 ') is null
# drop table T1
Go
# the Create table T1 ([name] nvarchar (21), [1] int)
Insert # T1
Union all select N 'a', 10
20 union all select N 'b',
30 union all select N 'c',
Select N 'd', 40
GO
If not object_id (N 'Tempdb for.. # T2 ') is null
Drop table # T2
Go
The Create table # T2 ([name] nvarchar (21), int value [2])
Insert # T2
20 union all select N 'a',
Select N 'c', 100 union all
Select N 'd', 50
Go
- the end of the test data
SELECT # T1. The name,
Value of 1,
Value 2
The FROM # T1
LEFT the JOIN # T2
ON # T2. Name=# T1. Name;



Excuse me, would you please tell me if the two table data volume is very big, how to write?

CodePudding user response:

refer to the second floor whc1199 response:
Quote: reference 1/f, February 16 response:

The test data
 -If not object_id (N 'Tempdb for.. # T1 ') is null 
# drop table T1
Go
# the Create table T1 ([name] nvarchar (21), [1] int)
Insert # T1
Union all select N 'a', 10
20 union all select N 'b',
30 union all select N 'c',
Select N 'd', 40
GO
If not object_id (N 'Tempdb for.. # T2 ') is null
Drop table # T2
Go
The Create table # T2 ([name] nvarchar (21), int value [2])
Insert # T2
20 union all select N 'a',
Select N 'c', 100 union all
Select N 'd', 50
Go
- the end of the test data
SELECT # T1. The name,
Value of 1,
Value 2
The FROM # T1
LEFT the JOIN # T2
ON # T2. Name=# T1. Name;



Excuse me, would you please tell me if the two table data volume is very big, how to write?
so write, but you can add index to associated fields

CodePudding user response:

Should use FULL feeling the JOIN
  • Related