MySQL Complicated Query

Discussion in 'PHP' started by HungryMinds, Aug 9, 2011.

  1. #1
    Hi Friend.

    I want a query to find some ids from field. But it's so difficult for me.

    table field name: product_ids

    product_ids (value): 1, 2, 3, 4, 5 (These all values are in one field with comma, It's a set of id's in one record field).

    Now i want to search 3 & 4 from this field.

    If user find 3 & 4, so i'll have to search 3 & 4 from product_ids

    I've tried "like" & "in"... But! still getting issue.

    Please help me out.

    I Tried E.g:

    1) select * from table_name where product_ids in (3, 4) // Not Working...

    2) select * from table_name where product_ids like '%3%' or product_ids like '%4%' // Not Working...

    Please help me out friends.
     
    HungryMinds, Aug 9, 2011 IP
  2. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #2
    I don't see any reason why your second query wouldn't have worked:

    SELECT * FROM table_name WHERE product_ids LIKE '%3%' OR product_ids LIKE '%4%'
    Code (markup):
    The only thing I can say is make sure that the table_name and column name are correct. Sorry :/
     
    Thorlax402, Aug 9, 2011 IP
  3. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #3
    Thanks for reply Thorlax402

    I've already tried this query

    When i'm executing your query (SELECT * FROM table_name WHERE product_ids LIKE '%3%' OR product_ids LIKE '%4%')

    It gets "33" behalf of '%3%' and "44" behalf of '%4%'

    It's not fetching exact match from field using like '%xyz%'

    Thanks again.
     
    HungryMinds, Aug 9, 2011 IP
  4. Youmax

    Youmax Active Member

    Messages:
    122
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Because the type is int and you can't do like exact-ing them..
     
    Youmax, Aug 9, 2011 IP
  5. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    Thanks for reply Youmax

    So then how should i make & execute this query? How could i match and then fetch 3 & 4 from record field?
     
    HungryMinds, Aug 9, 2011 IP
  6. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Your database is bad designed... you should create a new table to create the relationships... for example if you have a table called "new" and another called "category".
    new
    ----
    idnew
    title
    content

    category
    --------
    idcategory
    name

    If you want to create a "many to many" relationship you should create a table called "new_category" for example:

    new_category
    -------------
    idnew
    idcategory

    but you should not make some thing like:

    new
    ----
    idnew
    title
    categories_ids

    and save in categories_ids all categories ids...

    i know my english is bad i hope you undertand my point...
     
    elixiusx, Aug 9, 2011 IP
  7. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Anyway if you are not interested in follow my method you can use your method, i think you can do something like:

    SELECT * FROM table_name WHERE product_ids LIKE '% 3,%'

    So include the space before and the "," after...

    So if you have for example, 2, 12, 222, 432 and you are looking for the ID "2" SQL Will search for " 2,", if the space don't exists you can do something like:
    SELECT * FROM table_name WHERE product_ids LIKE '%,3,%' (comma before and comma afther the ID)

    Again excuse my english i hope you understand....
     
    elixiusx, Aug 10, 2011 IP
  8. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #8
    
    SELECT * FROM table_name WHERE FIND_IN_SET(3, product_ids) OR FIND_IN_SET(4, product_ids)
    
    Code (markup):
     
    Gray Fox, Aug 10, 2011 IP
    HungryMinds likes this.
  9. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #9
    Thanks for reply elixiusx
    You English is perfect :)
    Well i know database structure is not good, but i'll have to work on it :S
    Because we've done too many work with this database structure.
    Thanks for guide to restructure database.

    Your queries are good. but it will not work on every condition.
    Like if my record value is "3, 4, 5, 5" then we can't use this method '%,3,%'
    If my record value is "1, 2, 3" still we can't use this method '%,3,%' or '% 3,%'

    These are really logical queries or i think i'm beginner :)
    Please think more about it :)

    Thanks for your help.
     
    HungryMinds, Aug 10, 2011 IP
  10. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #10
    Thanks for reply Gray Fox
    Oh my GOD :)
    You've done a really great job.
    Now i'm going to give u star reputation :)
    Thanks a lot again friend.
    It really helped me out.
     
    HungryMinds, Aug 10, 2011 IP
  11. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #11
    Thanks for reply Gray Fox
    Oh my GOD :)
    You've done a really great job.
    Now i'm going to give u star reputation :)
    Thanks a lot again friend.
    It really helped me out.
     
    HungryMinds, Aug 10, 2011 IP
  12. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #12
    what about '% 3,%' or '%, 3' or '3, %'?
     
    elixiusx, Aug 10, 2011 IP
  13. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #13
    Thanks for your reply elixiusx

    '% 3,%' // it's perfect

    '%, 3' // It means 3 or 31 or more

    '3, %' // It means 3 or 13 or more

    :)
     
    HungryMinds, Aug 10, 2011 IP
  14. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #14
    '% 3,%' // it's perfect

    '%, 3' // It means 3 or 31 or more (Nope because i don't include a % at the end)

    '3, %' // It means 3 or 13 or more (Nope because i don't include a % at the begin)
     
    elixiusx, Aug 10, 2011 IP
  15. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #15
    So i think this method are correct...
     
    elixiusx, Aug 10, 2011 IP
  16. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #16
    I've tried both of them, Not Working On Every Condition :S
     
    HungryMinds, Aug 10, 2011 IP
  17. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #17
    Ah i think you are using only 1...

    The query must include the 3... so the query will be something like

    SELECT * FROM table_name WHERE product_ids LIKE '% 3,%' or product_ids LIKE '%, 3' or product_ids LIKE '3, %'
    So you need to include the 3 in the same query :)
     
    elixiusx, Aug 10, 2011 IP
  18. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #18
    Record Values: 1, 2, 3, 4, 5

    Query: SELECT * FROM ids WHERE pro_ids LIKE '%, 3'
    Result: No Record Found

    Query: SELECT * FROM ids WHERE pro_ids LIKE '3, %'
    Result: No Record Found
     
    HungryMinds, Aug 10, 2011 IP
  19. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #19
    Oh 3 Statements for just one match
    I've more than 10 values and all those i'll have to find from record :D
    10 Matches * 3 = 30 :D
     
    HungryMinds, Aug 10, 2011 IP
  20. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #20
    don't worry about it... are just some simple querys...
     
    elixiusx, Aug 10, 2011 IP