lisovaccaro
lisovaccaro

Reputation: 33946

Query two columns into one?

Is there a way to select these two colums:

Name 1    |      Name 2
John             Paul
Paul             Ringo
Ringo            George

So as to get both in one column with no repeated values:

Names
John
Paul
Ringo
George

Upvotes: 2

Views: 111

Answers (3)

Chris Felstead
Chris Felstead

Reputation: 1208

MySQL supports the UNION command. You just need to make sure the columns returned are the same.

E.g.

SELECT customerNumber id, contactLastname name
FROM customers
UNION
SELECT employeeNumber id,firstname name
FROM employees

Upvotes: 1

Michael Low
Michael Low

Reputation: 24506

SELECT name1 FROM theTable
UNION DISTINCT
SELECT name2 FROM theTable

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1499950

It sounds like you can use:

SELECT Name1 FROM Table1
UNION SELECT Name2 FROM Table1

That will perform duplicate row removal by default, or you can make it explicit like this:

SELECT Name1 FROM Table1
UNION DISTINCT SELECT Name2 FROM Table1

See the docs for UNION for more information.

Upvotes: 2

Related Questions