3

I have three columns as in the below image, and I would like to merge the two columns values1 and values2 into a single one. What is the SQL code to do it?

enter image description here

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    This is the same as http://stackoverflow.com/questions/22739841/mysql-combine-two-columns-into-one-column – dmitryro Jun 27 '16 at 16:14
  • 1
    Possible duplicate of [How to merge 2 or more columns into one?](http://stackoverflow.com/questions/31010993/how-to-merge-2-or-more-columns-into-one) – Muhammad Abdul Arif Sarker Jun 27 '16 at 16:17
  • 1
    I am not trying to concat two columns but i am trying to put one column on top of another –  Jun 27 '16 at 16:23

1 Answers1

1

Try a UNION query:

SELECT nouns, values1 FROM yourTable
UNION ALL
(
    SELECT NULL, values2 FROM yourTable
)

Demo here:

SQLFiddle

Update:

You could also use the nouns column instead of filling with NULL, e.g.

SELECT nouns, values1 FROM yourTable
UNION ALL
(
    SELECT nouns, values2 FROM yourTable
)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • this code combines the three columns into one but I would like to combine only values1 and values2 and keep nouns as a separate columns –  Jun 27 '16 at 16:22
  • @user123123123 I updated my answer. You can still use a `UNION` query I believe, but use `NULL` values as placeholders for missing values in the `nouns` column. – Tim Biegeleisen Jun 27 '16 at 16:25
  • one last thing please, how to duplicate the the values of column 'nouns' where it returns null –  Jun 27 '16 at 16:32
  • I will update my answer one last time, however I get the feeling that you are treating MySQL as if it is a glorified Excel, which it is not. – Tim Biegeleisen Jun 27 '16 at 16:46
  • 1
    I am new to sql but I was able to solve my problem using the code you showed me. thank you for your help –  Jun 27 '16 at 17:06