SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2008
    0 Post(s)
    0 Thread(s)

    Mysql exact match on sub string


    Here is the table structure:
    id (auto increment)
    keywords (Text)

    and values are:
    1 google,microsoft
    2 r,b
    3 america,argentina

    the keywords fieild has comma seperated values.
    If i want to search for R then i it should only return one row. i.e. 2nd row.

    but in my case, its returning all three rows because they have R in them.

    My sql query is:

    select * from table_name where keywords like 'r%'
    i also tried:
    select * from table_name where keywords like '%r'
    select * from table_name where keywords like '%r%'

    Can anybody tell me exact query that will return exact matched results.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Toronto, Canada
    63 Post(s)
    3 Thread(s)
    to answer your question...

    ... WHERE CONCAT(',',keywords,',') LIKE '%,r,%'

    however, your table design means that you will always do a table scan (inefficient) because searching with a leading wildcard means the database cannot use an index on that column

    it would be far better to redesign the table so that you are not storing a comma-delimited list in a single column | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Posting Permissions

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