简体   繁体   中英

How to join two tables together and return all rows from both tables, and to merge some of their columns into a single column

I'm working with SQL Server 2012 and wish to query the following:

I've got 2 tables with mostly different columns. (1 table has 10 columns the other has 6 columns). however they both contains a column with ID number and another column of category_name.

  1. The ID numbers may be overlap between the tables (eg 1 table may have 200 distinct IDs and the other 900 but only 120 of the IDs are in both).
  2. The Category name are different and unique for each table.

Now I wish to have a single table that will include all the rows of both tables, with a single ID column and a single Category_name column (total of 14 columns). So in case the same ID has 3 records in table 1 and another 5 records in table 2 I wish to have all 8 records (8 rows)

The complex thing here I believe is to have a single "Category_name" column.

I tried the following but when there is no null in both of the tables I'm getting only one record instead of both:

SELECT isnull(t1.id, t2.id) AS [id]
    ,isnull(t1.[category], t2.[category_name]) AS [category name]
FROM t1
FULL JOIN t2
    ON t1.id = t2.id;

Any suggestions on the correct way to have it done?

Make your FULL JOIN ON 1=0

This will prevent rows from combining and ensure that you always get 1 copy of each row from each table.

Further explanation:

A FULL JOIN gets rows from both tables, whether they have a match or not, but when they do match, it combines them on one row.

You wanted a full join where you never combine the rows, because you wanted every row in both tables to appear one time, no matter what. 1 can never equal 0, so doing a FULL JOIN on 1=0 will give you a full join where none of the rows match each other.

And of course you're already doing the ISNULL to make sure the ID and Name columns always have a value.

SELECT ID, Category_name, (then the other 8 columns), NULL, NULL, NULL, NULL FROM t1

UNION ALL

SELECT ID, Category_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, (then the other 4 columns) FROM t2

This demonstrates how you can use a UNION ALL to combine the row sets from two tables, TableA and TableB, and insert the set into TableC.

Create two source tables with some data:

CREATE TABLE dbo.TableA
(
    id int NOT NULL,
    category_name nvarchar(50) NOT NULL,
    other_a nvarchar(20) NOT NULL
);

CREATE TABLE dbo.TableB
(
    id int NOT NULL,
    category_name nvarchar(50) NOT NULL,
    other_b nvarchar(20) NOT NULL
);

INSERT INTO dbo.TableA (id, category_name, other_a)
    VALUES (1, N'Alpha', N'ppp'),
        (2, N'Bravo', N'qqq'),
        (3, N'Charlie', N'rrr');

INSERT INTO dbo.TableB (id, category_name, other_b)
    VALUES (4, N'Delta', N'sss'),
        (5, N'Echo', N'ttt'),
        (6, N'Foxtrot', N'uuu');

Create TableC to receive the result set. Note that columns other_a and other_b allow null values.

CREATE TABLE dbo.TableC
(
    id int NOT NULL,
    category_name nvarchar(50) NOT NULL,
    other_a nvarchar(20) NULL,
    other_b nvarchar(20) NULL
);

Insert the combined set of rows into TableC:

INSERT INTO dbo.TableC (id, category_name, other_a, other_b)
    SELECT id, category_name, other_a, NULL AS 'other_b'
        FROM dbo.TableA
    UNION ALL
    SELECT id, category_name, NULL, other_b
        FROM dbo.TableB;

Display the results:

SELECT *
    FROM dbo.TableC;

在此处输入图片说明

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