Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    1

    Unanswered: 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

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    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
    Home | Blog

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    george, fer cryin out pete's sake, you can't add strings

    what were you thinking

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Concat() ?
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •