If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Get column names according to condition

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-24-09, 12:00
Nayana Adassuriya Nayana Adassuriya is offline
Registered User
 
Join Date: Nov 2009
Posts: 1
Get column names according to condition

Hi all

i have a table as fallow in my mysql 5 database

Id | R | G | B
---------------
12 | 0 | 1 | 1
14 | 1 | 0 | 0
18 | 0 | 1 | 0
19 | 0 | 1 | 0

I want to get the column names that having value 1 where id = 12
the result should be 'G' and 'B'
So how can i write the query
Plz help soon

thanks
Nayana Adassuriya
Reply With Quote
  #2 (permalink)  
Old 11-24-09, 12:17
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
SELECT CASE WHEN R = 1 THEN 'R' END
     + CASE WHEN G = 1 THEN 'G' END
     + CASE WHEN B = 1 THEN 'B' END
FROM   your_table
WHERE  id = 12
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 11-24-09, 15:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
george, fer cryin out pete's sake, you can't add strings

what were you thinking

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 11-25-09, 08:33
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Concat() ?
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On