我在php的电子商务网站上工作。我有这样的多维php数组: -
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
)
我正在开发高级搜索选项。我有两个表,第一个是产品,另一个是productattributes。我想通过OR运算符内陷零索引数组值,一个索引数组值按OR
条件,然后是零索引和最终索引的最终数组,如下所示: -
select * from tbl_product where id IN(select product_id from tbl_vehicleproductequipments where (equipmentvalue_id = 1 OR equipmentvalue_id = 2 OR equipmentvalue_id = 3 OR equipmentvalue_id = 4) AND (equipmentvalue_id = 5 OR equipmentvalue_id = 6 OR equipmentvalue_id = 7)
我试过这段代码: -
$eqpcond = "";
if(!empty($_REQUEST["equipmentarr"])){
foreach($_REQUEST["equipmentarr"] as $y => $equipval){
$eqpcond = "select * from tbl_product where id IN (select product_id from tbl_vehicleproductequipments where ";
foreach($equipval as $s => $vl){
$equipcarr[] = " OR equipmentvalue_id = $vl";
}
}
if(!empty($equipcarr)){
$eqpcond = implode(" AND ",$equipcarr).")";
}
}
我得到了这样的查询,这是不正确的。
select * from tbl_product where id IN(select product_id from tbl_vehicleproductequipments where equipmentvalue_id = 1 AND OR equipmentvalue_id = 2 AND OR equipmentvalue_id = 3 AND OR equipmentvalue_id = 4 AND OR equipmentvalue_id = 5 AND OR equipmentvalue_id = 6 AND OR equipmentvalue_id = 7)
请帮助我,因为我遇到了这种情况,我不知道该怎么做。任何帮助将不胜感激。
先谢谢
答案 0 :(得分:1)
试试这个..我认为这会产生所需的查询。如果没有,请发布生成查询并进行必要的更改。
$eqpcond = "";
if (!empty($_REQUEST["equipmentarr"])) {
foreach ($_REQUEST["equipmentarr"] as $y => $equipval) {
$equipcstr = "";
$equipcarr = array();
$eqpcond = "select * from tbl_product where id IN (select product_id from tbl_vehicleproductequipments where ";
foreach ($equipval as $s => $vl) {
$equipcstr .= " OR equipmentvalue_id = $vl";
}
$equipcstr = trim($equipcstr, 'OR');
$equipcarr[] = $equipcstr;
}
if (!empty($equipcarr)) {
$eqpcond = implode(" AND ", $equipcarr) . ")";
}
}
答案 1 :(得分:0)
注意:我认为您没有有效的查询,
where equipmentvalue_id IN (1,2,3,4) AND equipmentvalue_id IN (5,6,7)
怎么可能?
检查出来:Online test
这是你想要的解决方案,使用一些内爆函数..
$arr = array(
array(1, 2, 3, 4),
array(5, 6, 7)
);
使用 OR
的子查询$sql = 'select product_id from tbl_vehicleproductequipments where (equipmentvalue_id = '.implode(" OR equipmentvalue_id = ", $arr[0]).') AND (equipmentvalue_id = '.implode(" OR equipmentvalue_id = ", $arr[1]).")";
使用 IN
的子查询$sql = 'select product_id from tbl_vehicleproductequipments where equipmentvalue_id IN ('.implode(",", $arr[0]).') AND equipmentvalue_id IN ('.implode(",", $arr[1]).')';
然后最后使用$sql
。
$main_sql = select * from tbl_product where id IN($sql);