Sorry I am really a newbie into programming and I am trying to merge two different columns into one column using sql but if it is not possible can it be done using sql code in c#? I have two tables Product1 and Product2, these tables both have CatID.
For Product1, the CatID contains
1
2
3
For Product2, the CatID contains
1
2
3
4
5
The results that I am getting using union is if they have both similar id it will be merge into one and using concat it will duplicate into like
1 1
2 2
3 3
4
5
But the results that I want is the 1 to 3 is from product1 and then the 4 to 8 from product2 like it will continue on counting with no duplicate:
1
2
3
4
5
6
7
8
Is this possible?
can you try this query, use union
select * from Product1
union
select * from Product2 NOT IN(SELECT CatID FROM Product1)
I am not sure what you want but with LINQ you can create a select. If you show us the SQL code it would be very helpful.
Try it like this:
SELECT CatID, ...OtherColumns...
FROM Product1
UNION SELECT CatID, ...OtherColumns...
FROM Product2
WHERE Product2.CatID NOT IN(SELECT CatID FROM Product1)
ORDER BY CatID
(test here: http://sqlfiddle.com/#!9/7887c ):
CREATE TABLE T1(ID INT, txt VARCHAR(10));
INSERT INTO T1 SELECT 1, 'test A'
UNION SELECT 2, 'test B'
UNION SELECT 3, 'test C';
CREATE TABLE T2(ID INT, txt VARCHAR(10));
INSERT INTO T2 SELECT 1, 'test 1'
UNION SELECT 2, 'test 2'
UNION SELECT 3, 'test 3'
UNION SELECT 4, 'test 4'
UNION SELECT 5, 'test 5';
SELECT ID, txt
FROM T1
UNION SELECT ID, txt
FROM T2
WHERE T2.ID NOT IN(SELECT ID FROM T1)
ORDER BY ID
The result:
ID txt
1 test A
2 test B
3 test C
4 test 4
5 test 5
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.