SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Multiple Filtering Against First Keyword?

    Hi,

    I am trying to filter all oak living room furniture. Current I can only filter oak coffee tables. But I am stuck on how to filter oak coffee tables and oak sideboards.

    Can anyone advise how I can filter but always using one keyword?



    This displays all oak coffee tables.

    Code:
    WHERE linkname LIKE '%oak%' and linkname LIKE '%coffee%'";
    This is displays nothing

    Code:
    WHERE linkname LIKE '%oak%' and linkname LIKE '%coffee%' and linkname LIKE '%sideboard%'";
    This displays all oak coffee tables and all sideboards of any material.

    Code:
    WHERE linkname LIKE '%oak%' and linkname LIKE '%coffee%' or linkname LIKE '%sideboard%'";

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    4 Thread(s)
    MySQL interprets the conditions from left to right, so it reads that last query as:

    Code MySQL:
    WHERE (linkname LIKE '%oak%' AND linkname LIKE '%coffee%') OR linkname LIKE '%sideboard%';

    You can change how the conditions are evalutated by using parenthesis, like this:
    Code MySQL:
    WHERE linkname LIKE '%oak%' AND (linkname LIKE '%coffee%' OR linkname LIKE '%sideboard%');

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your second ex precludes the result you desire simply because of bad logic. If item A is 'oak coffee tables' and item B is 'oak sideboard' then how you you query for 'oak' and 'coffee' and 'sideboard'?

    where linkname like '%oak%' and (linkname like '$coffee%' or linkname like '%sideboard%')

  4. #4
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried the following but it displays all oak. Should I be doing it like this?


    Code:
    WHERE linkname LIKE '%oak%' and (linkname like '$coffee%' or linkname like '%sideboard%')";

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sorry - I typed a $ in place of a % and you did a cut/paste. Correct and re-try.


Bookmarks

Posting Permissions

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