SQL "LIKE" question

Posted on 2012-08-10
Last Modified: 2012-08-10
Running MySQL 5+, Railo CF, Windows 2008 server

I want to search a field in my table for multiple keywords, and my question is what is the best way to handle this?

Here's an example of what I'm trying to do:

SELECT * FROM residential,seo WHERE residential.MLSNum=seo.MLSNum AND publiccomments LIKE '%foreclos%' OR publiccomments LIKE '%bank owned%' OR publiccomments LIKE 'REO' ORDER BY Price DESC

Is there a better way to handle this??

Question by:Bang-O-Matic
    LVL 8

    Accepted Solution

    I don't think there is any better way however you should enclose them all in brackets.

    AND (publiccomments LIKE '%foreclos%' OR publiccomments LIKE '%bank owned%' OR publiccomments LIKE 'REO')

    Also if the table is very large it will get slow unless you have a full text index which is only supported on a MyISAM table.
    LVL 1

    Assisted Solution

    the statement is good, i would just make the logical order clear this way:

    SELECT * FROM residential,seo WHERE (residential.MLSNum=seo.MLSNum) AND (publiccomments LIKE '%foreclos%' OR publiccomments LIKE '%bank owned%' OR publiccomments LIKE 'REO') ORDER BY Price DESC;

    in the last comparison you could also take publiccomments='REO' because there is no wildcard.

    if the tables are very big make sure that you have an index on the table holding the field publiccomments, otherwise the query can become slow if the tables are very big.

    Author Closing Comment

    Thanks! that's exactly what I needed.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now