Forum Moderators: open
Is there any way I can make a query to retrieve both columns as 1 big column, containing the records from column 1 followed by the records of column 2 (Thus this column would contain twice the amount of rows in the table) and then run a DISTINCT on that column to remove duplicates so that I can loop through the userIDs easily?
I am not sure how to go on about doing that.
CREATE TABLE tUser1 (
UserID VARCHAR(32) NOT NULL
)
CREATE TABLE tUser2 (
Username VARCHAR(32) NOT NULL
)
INSERT INTO tUser1 (UserID) VALUES ('Jim')
INSERT INTO tUser1 (UserID) VALUES ('Harry')
INSERT INTO tUser1 (UserID) VALUES ('Susan')
INSERT INTO tUser1 (UserID) VALUES ('Mary')
INSERT INTO tUser2 (Username) VALUES ('Jim')
INSERT INTO tUser2 (Username) VALUES ('Tom')
INSERT INTO tUser2 (Username) VALUES ('Rob')
INSERT INTO tUser2 (Username) VALUES ('Joel')
INSERT INTO tUser2 (Username) VALUES ('Susan')
INSERT INTO tUser2 (Username) VALUES ('Melissa')
SELECT UserID AS Username
FROM tUser1
UNION
SELECT Username AS Username
FROM tUser2
The query yields the following result set:
Username
--------------------------------
Harry
Jim
Joel
Mary
Melissa
Rob
Susan
Tom
(8 row(s) affected)
Notice 'Jim' and 'Susan' are not duplicated even though they were in both tables.