SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding AND clause to UNION - help

    Hi All,

    I'm trying to add an AND clause to a UNION query but unsure of what the syntax should be.

    Here is my UNION as is:

    Code:
    $result = mysqli_query($link,
    
    "(SELECT * FROM table_1 WHERE MATCH($field1) AGAINST('$keyword1*' IN BOOLEAN MODE))
    
    UNION 
    
    (SELECT * FROM table_2 WHERE MATCH($field2) AGAINST('$keyword2*' IN BOOLEAN MODE))");
    What I want to do is add an additional AND clause to both so:

    Code:
    $result = mysqli_query($link,
    
    "(SELECT * FROM table_1 WHERE MATCH($field1) AGAINST('$keyword1*' IN BOOLEAN MODE) AND MATCH($field2) AGAINST($array_selection))
    
    UNION 
    
    (SELECT * FROM table_2 WHERE MATCH($field2) AGAINST('$keyword2*' IN BOOLEAN MODE) AND MATCH($field2) AGAINST($array_selection))");
    Is this AND addition correct? Or, do I need to group the UNION, then supply an AND clause? Nothing is working quite yet...

    Thanks!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,262
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by swto View Post
    Is this AND addition correct?
    what happened when you tested it?


    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bit of a delayed response -

    When I run this I get a "undefined index" for $array_selection. I assume this is because the array (from checkboxes) is not always going to contain something.

    I think the solution would be to create a dynamic UNION query. But, while I know can do this with a regular-old SELECT, I don't know how to approach it when running a UNION query.

    Any suggested reading?
    Thanks!


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
  •