I have this array:
$results = array('1', '3', '123')
and this table:
id | item | tag
1 1 1
2 1 3
3 1 123
4 2 1
5 3 1
6 4 3
I want to perform a query like this:
SELECT item FROM table WHERE ALL 3 TAGS OF THE ARRAY EXIST.
In the example I want to return only Item=1, because it’s the only Item with all 3 tags of the array.
So far I have this:
$query .= " SELECT item FROM #__table WHERE tag IN ('";
$query .= implode("','",$results);
$query .= "')";
But it does not return the correct Items, instead it returns all items that are related at least with one element of the array.
To make the IN work as an AND instead of an OR, you use a HAVING clause to make sure that you get the same number of distinct values returned as exist in the IN clause.