MySQL Query not working as intended

Discussion in 'PHP' started by PHPGator, Jan 20, 2010.

  1. #1
    Here is the exact query (copy and pasted)

    SELECT *
    FROM wp_posts
    WHERE post_content LIKE '%Jon 1%'
    OR post_content LIKE '%Jonah 1%'
    OR post_content LIKE '%Jon. 1%'
    OR post_content LIKE '%Jona 1%'
    OR post_content LIKE '%Jo 1%'
    AND post_status = 'publish'
    AND post_type = 'post'
    Code (markup):
    The problem is that it is still pulling results with a post_status of "inherit" and post_type with 'page'. I want only results with publish as the post_status and "post" as the post_type.

    Any idea why it would be doing this? Is it the "OR's"?
     
    PHPGator, Jan 20, 2010 IP
  2. PHPGator

    PHPGator Banned

    Messages:
    4,437
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    260
    #2
    Whoops! those should have been AND's not OR's... ;) Please remove thread, fixed.
     
    PHPGator, Jan 20, 2010 IP
  3. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #3
    replaceing AND for the ORs wont work ether

    SELECT *
    FROM wp_posts
    WHERE (post_content LIKE '%Jon 1%'
    OR post_content LIKE '%Jonah 1%'
    OR post_content LIKE '%Jon. 1%'
    OR post_content LIKE '%Jona 1%'
    OR post_content LIKE '%Jo 1%')
    AND post_status = 'publish'
    AND post_type = 'post'
    Code (markup):
    add brackets to make the like statement one statment - if you replace with ANDs then the postcomment will have to be like all the terms at once
     
    WebWorth, Jan 20, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Hi,
    It's better using regexp instead of so many like.
    Regards,
    Nick
     
    koko5, Jan 20, 2010 IP