SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot daemon's Avatar
    Join Date
    Apr 2001
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can I join more than 3 statements in the WHERE clause of a mysql query?
    Something like:
    PHP Code:
    select from table where thread=and subject like "$searchtextor text like "$searchtext"" 
    Now the searchtext querys should be joined together.
    i.e.: I need all sets where subject or text contains searchtext but wherever searchtext is found the thread must be 0.
    I tryed to use brackets like this:
    PHP Code:
    select from table where thread=and (subject like "$searchtextor text like "$searchtext")
    but it doesn't work, or at least doesn't provide the result I'm looking for.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    First, I would try to see if your quotes in the interior of your query are causing the problems. You have to escape them.

    I am not sure how to force groupings when you are using logical expressions. It seems like you should be able to use parenthesis, but it looks like maybe the first two expressions are being ANDed together, which isn't what you want, so I would try putting the stuff in parenthesis first, like this:

    $sql = "select * from table where subject like \"$searchtext\" or text like \"$searchtext\" and thread=0";

    That way the OR will be evaluated first.
    Last edited by 7stud; May 1, 2001 at 03:49.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This just in from the www.mysql.com website:

    7.4.1 Grouping Functions
    ( ... )
    Parentheses. Use these to force the order of evaluation in an expression:
    mysql> select 1+2*3;
    -> 7
    mysql> select (1+2)*3;
    -> 9

    So, your parenthesis in your expression shouldn't be causing any problems.

  4. #4
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your result will only return results if the subject exactly = the search text or the text does.

    So if you're searching for 'peaches'

    text or subject needs to equal 'peaches' and nothing more

    if you want to search for peaches and the text =

    'I think peaches are good'

    then you need to add %'s around your search term.

    In SQL % signs are wildcard figures - so you ad them when you want to search by part of the column or a column's contents rather than the whole column.

    where text like '%$searchterm%'

    as another example. If you had a list of names and wanted to return everyone whose name started with A you could do this.

    where name like 'A%'

    in this case you only put the wildcard on one side because you know you want it to start with an A.

    Get it?
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums


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
  •