我有一个产品表、一个自定义定义表和一个值表:
产品
id product_name price
48 product one 32.99
87 another product 2.50
...etc
字段定义
id name
6 brand
9 colour
23 flavour
...etc
字段值
product_id fielddef_id value
48 6 Rowntree
48 9 Red
48 23 Strawberry
87 6 Cadburys
87 9 Yellow
87 23 Lemon
etc...
我现在正在尝试使用带有每个 fielddef 的下拉选择的过滤器进行搜索:
$sql = "SELECT * FROM products INNER JOIN fieldvals ON fieldvals.product_id=products.id WHERE product_name LIKE '%".$q."%' AND (fielddef_id='7' AND value='".$c."' ) AND (fielddef_id='8' AND value='".$b."') AND (fielddef_id='9' AND value='".$f."') AND (fielddef_id='10' AND value=' ".$t."') AND (fielddef_id='5' AND value='".$h."') AND (fielddef_id='6' AND value='".$v."')";
SQL的回声给了我类似的东西:
SELECT * FROM products INNER JOIN fieldvals ON fieldvals.product_id=products.id WHERE product_name LIKE '%cola%' AND (fielddef_id='7' AND value='Purple') AND (fielddef_id='9' AND value='Apple' ) AND (fielddef_id='10' AND value='Party') AND (fielddef_id='5' AND value='true') AND (fielddef_id='6' AND value='true')
此代码仅适用于 $q 之后的一个条件 - 即您可以包含 $q 和 $c,或 $q 和 $h,或 $q 和 $v,并且它成功找到匹配的产品,但多个嵌套 AND 语句不似乎工作。
我哪里错了?