MySQL/PHP -查询多个表来查看记录是否匹配数组中的数字


MySQL/PHP - query multiple tables to see if records match number in array

我有一个带有复选框的表单,我在提交表单时将其作为数组抓取:

<input name='price[]' type='checkbox' value='1'/>
<input name='price[]' type='checkbox' value='2'/>
<input name='price[]' type='checkbox' value='3'/>
<input name='price[]' type='checkbox' value='4'/>

然后获取数组中的值并像这样查询DB:

$priceArray = $_POST['price'];
$selectPrice = join(',',$priceArray);
"SELECT DISTINCT community FROM community_prices WHERE prices IN ($selectPrice)"

这工作得很好,但我需要对3个额外的数组和3个额外的表做同样的事情,我想从同一个查询中完成所有这些。

所以例如:

表1 community_prices有以下字段:id,community,prices

表2 community_amenities有以下字段:id,community,amenities

表3 community_demographic有以下字段:id,community,demographic

表4 community_products有以下字段:id,community,product

我该怎么做呢?

UNION可能是您的朋友。

使用JOIN从多个表中进行选择,例如

SELECT "YOUR FIELDS" FROM table1 
    JOIN table2 ON  table2.prices IN ($selectPrice)
    JOIN table3 ON  table3.community = table1.comumnity
    JOIN table4 ON  table4.community = table3.comumnity
GROUP BY "YOUR FIELD"
ORDER BY "YOUR FIELD"