Search product name both ways?

Discussion in 'MySQL' started by greatlogix, Feb 4, 2009.

  1. #1
    I am using following query to search product names for the keywords submitted by customers.
    
    select * FROM tblproducts WHERE  prodName LIKE lower('%$keyword%')
    
    Code (markup):
    I have product name in db "Crib Green" When i provide keyword "crib green" query returns one row.Its perfect. When i search for "Green Crib" search result is zero row selected.

    I want that MySQL should return "Crib Green" whether I search "crib green" or "green crib"

    How? Please help.

    Thanks
     
    greatlogix, Feb 4, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    you should do a query like
    ALTER TABLE tblproducts ADD FULLTEXT(prodName);
    select * FROM tblproducts WHERE match (prodName) against ($keyword)
     
    crivion, Feb 4, 2009 IP
    greatlogix likes this.
  3. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    select * FROM tblproducts WHERE prodName (LIKE lower('%$keyword1%')) AND (LIKE lower('%$keyword2%'))
    You should split you search request into seperate items and offer them this way.

    P.S. Using crivion's suggestion is usually beter since the responsibility for searching is left to the database server instead of doing it yourself.
     
    chisara, Feb 5, 2009 IP
    greatlogix likes this.
  4. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #4
    Thanks crivion & chisara for your kind help. FULLTEXT worked for me. Rep added for both.
     
    greatlogix, Feb 6, 2009 IP