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:

$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:

$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!

what happened when you tested it?

:slight_smile:

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!