[eluser]vitoco[/eluser]
when i'm not sure how many values am i adding ( as conditions ) to the query, i do this :
Code:
// HERE IS HARCODED, BUT YOU CAN ADD ITEMS ( CONDITIONS ) TO THE ARRAY OF TAGS INSIDE YOUR "IFs"
$tags = array( 40 , 19 , 15, 0 ) ;
//
$sql = "
SELECT
* ,
COUNT( tag_id ) AS num_tags ,
GROUP_CONCAT( tag_id ) AS tags
FROM
restaurants
INNER JOIN
tag_restaurants_rel
ON
tag_restaurants_rel.restaurant_id = restaurants.restaurant_id
WHERE
tag_restaurants_rel.tag_id IN ( ".implode( ',' , $tags )." )
GROUP BY
restaurants.restaurant_id
HAVING
num_tags = ".count( $tags )."
ORDER BY
rest_name asc
" ;
The result, a dynamic number of tags to match, the only limitation is that it must be at least one,if not the query resulting is invalid
Code:
....
WHERE
tag_restaurants_rel.tag_id IN () <--- SQL SYNTAX ERROR
GROUP BY
restaurants.restaurant_id
HAVING
num_tags = 0
...
Saludos