Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Finding common values

    I have a table with two columns: imageID and vendorID.

    I'm wondering whats the best way to find all the common vendorIDs among a set of imageIDs (the two make up the primary key for the table, so there is only one pair combination each).

    So if I have:

    imageID | vendorID
    1 | 1
    2 | 1
    1 | 2
    3 | 3

    What would the best way to get back the common vendorIDs among imageID 1 and 2? (so to get back 1?)

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Code:
    SELECT t1.vendorID, t1.imageID AS image1, t2.imageID AS image2
    FROM tablename AS t1, tablename AS t2
    WHERE t1.imageID < t2.imageID
    AND t1.vendorID = t2.vendorID
    NOTE: If two vendorIDs have *three* imageIDs in common you will get three results.

    Example:
    Code:
    imageID | vendorID
          1 | 1
          2 | 1
          3 | 2
          4 | 1
    You would get back
    Code:
    vendorID | image1 | image2
           1 |      1 |     2
           1 |      1 |     4
           1 |      2 |     4
    So an alternative answer might be
    Code:
    SELECT vendorID, GROUP_CONCAT( imageID SEPARATOR ',' ) AS images
    FROM tablename
    GROUP BY vendorID
    And that would give you back
    Code:
    vendorID | images
           1 | 1,2,4
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Sorry if i was unclear, as I'm not sure that tackles my problem:

    If I'm giving a set of imageIDs, how can I figure out what vendors they have in common? Right now, I'm doing this:

    Code:
    SELECT vendorID, COUNT(*) numImage
    FROM imageVendors
    WHERE imageID IN (1, 2)
    GROUP BY vendorID
    HAVING numImage = 2
    This should yield me back all the vendorIDs that 1 and 2 have, but only display the ones they both have. I'm just trying to figure out if I can be more efficient.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Looks pretty good to me.

    You could do it via a JOIN, but I doubt that would be faster.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ok, sounds good! Thanks.


  •  

    Posting Permissions

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