Looking up id that matches multiple values (MySQL)

Discussion in 'MySQL' started by thing2b, Nov 20, 2008.

  1. #1
    I have a simple lookup table (MySQL):

    id1 int 11
    id2 int 11

    Example data is:

    id1 / id2
    1 / 1
    1 / 2
    1 / 3
    2 / 2
    2 / 3
    2 / 4

    What I would like is to find the value of id1 where the values of id2 match a set of values (like 2, 3 AND 4).

    How can I get the id1 of 1 returned given a set of values (2, 3, 4) to match against id2?
     
    thing2b, Nov 20, 2008 IP
  2. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If I understand the question, you want an IN. Using your example data i created this table (dp_112208):
    +------+------+
    | id1  | id2  |
    +------+------+
    |    1 |    1 | 
    |    1 |    2 | 
    |    1 |    3 | 
    |    2 |    2 | 
    |    2 |    3 | 
    |    2 |    4 | 
    +------+------+
    Code (markup):
    And using the query:
    SELECT id1 FROM dp_112208 WHERE id2 IN (2,3,4);
    Code (markup):
    I get this result:
    +------+
    | id1  |
    +------+
    |    1 | 
    |    1 | 
    |    2 | 
    |    2 | 
    |    2 | 
    +------+
    Code (markup):
    I hope this helps!
     
    drunnells, Nov 22, 2008 IP