MySQL: Using value of field for WHERE IN

Discussion in 'MySQL' started by chopsticks, Oct 11, 2008.

  1. #1
    I know this is probably one of the worst ways to do something like this in SQL but it's just for a temporary (bad) band-aid solution which will be fixed later on.

    Basically, at the moment I have a field where data is stored as such: subscription_type = '1,3,7,8'. Based on that I need to use that data in a select statement, where that data is used in a WHERE IN clause.
     
    chopsticks, Oct 11, 2008 IP
  2. kkrawal

    kkrawal Peon

    Messages:
    235
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    is your data in field type Char ?
    It meas for example field name is fieldXYZ and there will be data '1,3,7,8' and '1,5,7' and '5,7,1,5' as on..
    ?
    and you need to select '1' or '1,3' from fieldXYZ

    pm me
     
    kkrawal, Oct 14, 2008 IP
  3. jgarrison

    jgarrison Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here an example:
    SELECT column1, column2, column3
    FROM table
    WHERE somecolumn IN (1,3,7,8);

    If somecolumn is a character field and not a numeric field then write it like this:
    SELECT column1, column2, column3
    FROM table
    WHERE somecolumn IN ('1','3','7','8');

    -Jim
     
    jgarrison, Oct 18, 2008 IP