Jump to content

Is it possible to get several columns to stack in single result column?


tarsier

Recommended Posts

Hello All,

I have a SQL question and hope someone can point me in the right direction. I would like to do a query and have several columns reduce into a single result column. I will try to give a simple example here.

 

I have a table that looks something like this

 

account_name

account_phone1

account_phone2

name1

555-555-1111

555-555-2222

name2

555-555-3333

555-555-4444

 

 

I would like to do a search like:

SELECT {SQL MAGIC} WHERE account_name = 'name1'

 

and get the result

 

account_name

phone

name1

555-555-1111

name1

555-555-2222

 

Where phone1 and phone2 are combined into a single column. Is this possible? It will greatly reduce the complexity of my program.

 

Thanks for any input you have.

 

- Dave

 

 

Link to comment
Share on other sites

There are several ways to do this, but probably the easiest would be a union

 

SELECT account_name, account_phone1 AS phone
FROM account
WHERE account_name = 'name1'

UNION ALL

SELECT account_name, account_phone2
FROM account
WHERE account_name = 'name1'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.