我有这张桌子:
ID | Product_ID | Filter_ID
1 1 8
2 1 9
3 2 10
我还有php数组包含要检查的过滤器:
$arr = (8,9,10);
现在,如何确保Product_ID(1)具有$ arr中的所有过滤器?
答案 0 :(得分:0)
如果您只想要那些包含所有3个filter_id的product_id,请尝试:
$sql = "SELECT COUNT(*) AS total, Product_ID FROM `test`.`test` WHERE Filter_ID IN (".implode(', ', $arr).") GROUP BY Product_ID HAVING total >= ". sizeof($arr);
http://sqlfiddle.com/#!2/e949f/7/0
<强> @edit 强>
如果您只需知道特定的Product_ID是否具有全部3个Filter_IDS:
$sql = "SELECT COUNT(*) AS total, Product_ID FROM `test`.`test` WHERE Filter_ID IN (".implode(', ', $arr).") and Product_ID = 1 GROUP BY Product_ID HAVING total >= ". sizeof($arr);
答案 1 :(得分:-1)
$arr = [8, 9, 10];
$sql = "SELECT * FROM `Table` WHERE `Filter_ID` IN (".implode(', ', $arr).")";