简体   繁体   中英

How to Join two SELECT queries having same column names but different row values in both tables

I got a table from SELECT Query 1 as below from SQL SERVER:

NUMBER NAME Date
21 Name1 20.03.2004
25 Name2 26.06.2005
23 Name3 26.06.2005
24 Name4 22.04.2012

I got a table from SELECT query 2 as below from SQL SERVER:

NUMBER NAME Date
30 Name10 20.03.2064
30 Name10 26.06.2035
35 Name30 26.06.2025
36 Name40 22.04.2042

I want to join these SELECT queries into one SELECT query like below

NUMBER NAME Date
21 Name1 20.03.2004
25 Name2 26.06.2005
23 Name3 26.06.2005
24 Name4 22.04.2012
30 Name10 20.03.2064
30 Name10 26.06.2035
35 Name30 26.06.2025
36 Name40 22.04.2042

I tried like this

Select * from ( select Number,Name,Date from table1 ) t1 
inner join ( select Number, Name, Date from table2) t2
on t1.number = t2.number

But it didnt work, This is not a actual table i want to join.

Basically I want to join two SELECT Query who got same Column names but have no common values between them. And I want to use SELECT query from the joined table.

Thank you

    SELECT Number, Name, Date FROM table1
    UNION ALL
    SELECT Number, Name, Date FROM table2

The output as per the question is not related to SQL JOIN , Rather It can be achieved by UNION or UNION ALL

Anyway, You can use UNION to select both tables into single table. UNION can work if the data type and column count of the tables are same and equal respectively.

The solution for your problem would be:

SELECT [NUMBER], [NAME], [Date] FROM table1
UNION ALL
SELECT [NUMBER], [NAME], [Date] FROM table2;

Some the basic difference of UNION and JOIN are:

JOIN UNION ALL
JOIN combines data from many tables based on a matched condition between them. SQL combines the result-set of two or more SELECT statements.
It combines data into new columns. It combines data into new ROWS
Number of columns selected from each table may not be same. Number of columns selected from each table should be same.
Datatypes of corresponding columns selected from each table can be different. Datatypes of corresponding columns selected from each table should be same.

UNION and UNION ALL are SQL operators used to concatenate 2 or more result sets. This allows us to write multiple SELECT statements, retrieve the desired results, then combine them together into a final, unified set.

The main difference between UNION and UNION ALL is that:

UNION: only keeps unique records
UNION ALL: keeps all records, including duplicates

UNION Example:

SELECT column1 AS datacheck from table1 
  UNION 
SELECT column1 AS datacheck from table2

Result:

+-----------+
| datacheck |
+-----------+
|   data2   |
+-----------+

UNION ALL example:

SELECT column1 AS datacheck from table1 
  UNION ALL
SELECT column1 AS datacheck from table2

Result:

+-----------+
| datacheck |
+-----------+
|   data2   |
|   data2   |
+-----------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM