MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 29th, 2008, 03:50 PM
Stormybot Stormybot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 11 Stormybot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 37 m 35 sec
Reputation Power: 0
Selecting multiple columns as one column

Hi everyone. First time posting. I did a search and could not find what I was looking for. Hopefully someone can answer this.

I am trying to SELECT multiple columns as one column for display. Something like this (which obviously does not work):

Quote:
SELECT column1 as combinedlist, column2 as combinedlist FROM `myTable` WHERE column1='data1' OR column2='data1' ORDER BY combinedlist


I have also tried:

Quote:
SELECT (column1, column2) as combinedlist FROM `myTable` WHERE column1='data1' OR column2='data1' ORDER BY combinedlist


Am I even close?

Reply With Quote
  #2  
Old January 29th, 2008, 03:57 PM
jcarouth's Avatar
jcarouth jcarouth is offline
Never give up
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Sep 2005
Location: College Station, TX, USA
Posts: 2,625 jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)  Folding Points: 1251 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 12 h 56 m 2 sec
Reputation Power: 807
Facebook
Code:
select concat(column1, ', ', column2) as combinedlist
from `myTable`
where 'data1' in(column1, column2)
order by combinedlist
__________________
Jeff Carouth's Homepage

Reply With Quote
  #3  
Old January 29th, 2008, 04:01 PM
Stormybot Stormybot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 11 Stormybot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 37 m 35 sec
Reputation Power: 0
Quote:
Originally Posted by jcarouth
Code:
select concat(column1, ', ', column2) as combinedlist
from `myTable`
where 'data1' in(column1, column2)
order by combinedlist


Sorry, I should have explained better. I do not actually want it listed as "column1, column2" in the results. Maybe an example would be good.

Say column1 had:
Bob
Steve
Jen
Valerie

Say column2 had:
David
Kevin
Miko
Walt

I want my resulting combinedlist to have:
Bob
David
Jen
Kevin
Miko
Steve
Valerie
Walt

Reply With Quote
  #4  
Old January 29th, 2008, 04:04 PM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,683 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 19 h 35 m 2 sec
Reputation Power: 284
Code:
SELECT column1
FROM myTable
UNION ALL
SELECT column2
FROM myTable

Reply With Quote
  #5  
Old January 29th, 2008, 04:05 PM
jcarouth's Avatar
jcarouth jcarouth is offline
Never give up
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Sep 2005
Location: College Station, TX, USA
Posts: 2,625 jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)  Folding Points: 1251 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 12 h 56 m 2 sec
Reputation Power: 807
Facebook
Code:
select column1 as name
from your_table

union

select column2
from your_table

order by name

Reply With Quote
  #6  
Old January 29th, 2008, 04:07 PM
jcarouth's Avatar
jcarouth jcarouth is offline
Never give up
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Sep 2005
Location: College Station, TX, USA
Posts: 2,625 jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)jcarouth User rank is Major General (70000 - 90000 Reputation Level)  Folding Points: 1251 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 12 h 56 m 2 sec
Reputation Power: 807
Facebook
note that UNION vs UNION ALL will give you no duplicates for the former and duplicates for the latter (e.g. if David is in both column1 and column2 then using UNION you will have only one 'David' in the results whereas UNION ALL will give you two or more).

Reply With Quote
  #7  
Old January 29th, 2008, 04:15 PM
Stormybot Stormybot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 11 Stormybot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 37 m 35 sec
Reputation Power: 0
That worked perfectly! Thank you everyone! And thanks jcarouth
for the explanation between UNION and UNION ALL. This forum is great.

Reply With Quote
  #8  
Old October 26th, 2012, 11:04 PM
karnival800 karnival800 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 35 karnival800 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
a question on counting unions

lets say i wanted to COUNT total occurrences in multiple columns how would i do that?

for example

Col1
David
Joe
Mary
David

Col2
Chris
Jack
Joe

to make this
David 2
Joe 2
Mary 1
Chris 1
Jack 1

Reply With Quote
  #9  
Old October 27th, 2012, 01:06 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,359 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 5 h 19 m 29 sec
Reputation Power: 4140
Quote:
Originally Posted by karnival800
lets say i wanted to COUNT total occurrences in multiple columns how would i do that?
Code:
SELECT name
     , COUNT(*)
  FROM ( SELECT column1 AS name
           FROM daTable
         UNION ALL
         SELECT column2
           FROM daTable ) AS u
GROUP
    BY name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Selecting multiple columns as one column

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap