Skip to main content
Known Participant
November 17, 2010
Question

Find a string within a field

  • November 17, 2010
  • 1 reply
  • 1664 views

This may be easy but I am new to this so here it goes.  I am trying to find if a string can be found in a mysql database field.

The following will only find exactly match

    <CFQUERY DATASOURCE="databasename">
    SELECT mycontact, userid
    FROM mytable
    WHERE email = #form.email#'

    </CFQUERY>

The following will return result if the field email is contain in the form.email

    <CFQUERY DATASOURCE="databasename">
     SELECT mycontact, userid
     FROM mytable
     WHERE email like '%#form.email#%'

    </CFQUERY>

But these will not give what I want.  I want the reverse of the above.  The email fied can contain MORE than one email addresses.  I want to be able to enter a value i.e. form.email and search the table and find any record in the "email" field that contain form.emal.  Help is appreciated.  I am using MYSQL database.  Thanks.

This topic has been closed for replies.

1 reply

Inspiring
November 17, 2010

Set up a one to many relationship between users and email addresses and it will be a snap.  If you don't know what that means, I've heard good things about the book, Database Design for Mere Mortals.

jackhuangAuthor
Known Participant
November 17, 2010

You probably mean one user with many emails.  But that will require changing database with a table for userid and emails.  I am trying to avoid that.  So all the user emails are in a filed.  The emails in the 'email' field is separated by comma.  The LIKE won't work either because I don't want to retrieve more than one.  If someone enter cd@gmail.com, I don't want to get abcd@gmail.com, bcd@gmail.com etc.   That is the difficult part.

Participating Frequently
November 17, 2010

I think you have 4 cases:

- there's a single email;

- email is at the start of the list;

- email is in the middle of the list;

- email is at the end.

Those 4 cases translate roughly into these conditions:

email_list LIKE '#email#' OR email_list LIKE '#email#,%' OR email_list

LIKE '%,#email#,%' OR email_list LIKE '%,#email#'

Horrible for performance if you have lots of emails but should work.

--

Mack