SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    MYSQ & Comma Seperated values

    Hello,

    empno | loc_ids [varchar]
    ----------------------
    38084 | 12,10,20
    38085 | 18,12

    Now if issue the following query

    Code:
    SELECT *
    FROM `table`
    WHERE `loc_ids` =12
    it returns 38084. but i feel this is wrong
    how come "12,10,20" = 12?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,411
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    It isn't. I'm sure that query doesn't return that row if the data you posted here is correct.
    Are you sure there isn't a row in that table with loc_ids = '12' ?

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    It isn't. I'm sure that query doesn't return that row if the data you posted here is correct.
    Are you sure there isn't a row in that table with loc_ids = '12' ?
    Hai guido,

    pls see these screen shots :
    http://i.imgur.com/SVy3Zsb.jpg
    http://i.imgur.com/ntBIkkW.jpg

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,411
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Maybe it has to do with the column being varchar, and the value being numeric.
    Try this:
    Code:
    SELECT *
    FROM `table`
    WHERE `loc_ids` = '12'
    You might want to consider normalizing that table though.

  5. #5
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Maybe it has to do with the column being varchar, and the value being numeric.
    Try this:
    Code:
    SELECT *
    FROM `table`
    WHERE `loc_ids` = '12'
    You might want to consider normalizing that table though.
    absolutely. that is the reason. Wrapping up that 12 returns 0 records.

    btw, normalizing mean?
    but with FIND_IN_SET() i have no problem.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,411
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by afridy View Post
    btw, normalizing mean?
    Normalization: http://en.wikipedia.org/wiki/Database_normalization

    In this case, instead of storing multiple location ids separated by comma in a column, you'd have one empno-locid pair per row:

    empno | loc_id
    ----------------------
    38084 | 12
    38084 | 10
    38084 | 20
    38085 | 18
    38085 | 12

    Easier to maintain (what do you do if you need to remove loc_id 10 from empno 38084?), and easier to query.

  7. #7
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Normalization: http://en.wikipedia.org/wiki/Database_normalization

    In this case, instead of storing multiple location ids separated by comma in a column, you'd have one empno-locid pair per row:

    empno | loc_id
    ----------------------
    38084 | 12
    38084 | 10
    38084 | 20
    38085 | 18
    38085 | 12

    Easier to maintain (what do you do if you need to remove loc_id 10 from empno 38084?), and easier to query.
    Thank you for the valuable advise!

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,021
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by afridy View Post
    but with FIND_IN_SET() i have no problem.
    however, this will always require a table scan, so it gets slower and slower the more rows you have
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •