SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Location
    Sydney, Australia
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL: Find if a value is in a list of values

    Hi all,

    I have a text field that contains a list of IDs e.g. 16,20,24,29,38,40

    I need to find any records where that field contains a particular ID.

    I thought that something like SELECT * FROM tablename WHERE IDlistfield CONTAINS myID but that doesn't work.

    Can anyone point me in the right direction?

    TIA

    Tom

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What you should do is get rid of multiple values separated by commas in a column.

    But something like this should work:
    Code:
    SELECT * 
    FROM tablename 
    WHERE IDlistfield LIKE '16%'
    OR IDlistfield LIKE '%,16,%'
    OR IDlistfield LIKR '%,16'

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It would be better to change your data model to avoid putting comma separated values into a field. Searching will be easier. The code above is almost correct but if you have a number 16x then this will be accepted. Try this:

    Code:
    SELECT * 
    FROM tablename 
    WHERE IDlistfield = '16'
    OR IDlistfield LIKE '16,%'
    OR IDlistfield LIKE '%,16,%'
    OR IDlistfield LIKR '%,16'

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rcashell View Post
    The code above is almost correct but if you have a number 16x then this will be accepted.
    Ah yes, I forgot the , after the 16. And I forgot the 16 as a single value too. Good catch!

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    WHERE FIND_IN_SET('16',IDlistfield) > 0

    but all of these solutions are slow, and will not scale -- the more rows in the table, the slower the query gets

    you should normalize that data
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Location
    Sydney, Australia
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to everyone for your replies. I wish I could rearrange the data but I'm stuck with it.

    I'll check out your book Rudy.

    Cheers


Tags for this Thread

Bookmarks

Posting Permissions

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