使用foreach循环的结果不是我想要的

时间:2018-11-22 08:35:59

标签: php sql join

$warehousesArray = array('Paharganj' => '5',
                         'Sant Nagar'=>'11324',
                         'Mumbai'=>'35798',
                        'UP Border'=>'117262',
                        'Top Floor'=>'170740',
                        'Gurgaon'=>'170741');


$stockSummaryQuery1 = "   SELECT 
                p.productid,
                                p.productname,
                                scf.cf_976 AS stock_quantity,
                                w.warehousename,
                                SUM(IF(scf.cf_974=scf.cf_976 AND scf.cf_974 != 0,1,0)) AS number_of_packs
                                FROM vtiger_products AS p
                                LEFT JOIN vtiger_productcf AS pcf ON pcf.productid = p.productid
                                LEFT JOIN vtiger_stockscf AS scf ON scf.cf_922 = p.productid
                                LEFT JOIN vtiger_stocks AS s ON scf.stocksid = s.stocksid
                            JOIN vtiger_crmentity AS stockent ON stockent.crmid = s.stocksid
                                    AND stockent.deleted = 0
                    LEFT JOIN vtiger_racks AS ra ON ra.racksid = scf.cf_924
                            JOIN vtiger_rackscf AS racf ON ra.racksid = racf.racksid
                    JOIN vtiger_rooms AS ro ON ro.roomsid = racf.cf_900
                            JOIN vtiger_roomscf AS rocf ON rocf.roomsid = ro.roomsid
                    JOIN vtiger_warehouses AS w ON rocf.cf_910 = w.warehousesid
                                LEFT JOIN vtiger_vendor AS v ON v.vendorid = p.vendor_id WHERE 1 ";

if (isset($_GET['vendor_name']) && !empty($_GET['vendor_name'])) {
                $warehouseid_array = explode("_", $_GET['vendor_name']);
                $warehouseid = $warehouseid_array[0];
                $stockSummaryQuery1 .= " AND v.vendorid = ".$warehouseid;
            }

foreach ($warehousesArray as $key => $value) {

                    echo  $stockSummaryQuery1.= " AND w.warehousesid = $value";
            echo "<br><br><br><br>";


}
$stockSummaryQuery1 .= " GROUP BY p.productid ASC";

我回显了查询,结果为

SELECT p.productid, p.productname, scf.cf_976 AS stock_quantity, w.warehousename, SUM(IF(scf.cf_974=scf.cf_976 AND scf.cf_974 != 0,1,0)) AS number_of_packs FROM vtiger_products AS p LEFT JOIN vtiger_productcf AS pcf ON pcf.productid = p.productid LEFT JOIN vtiger_stockscf AS scf ON scf.cf_922 = p.productid LEFT JOIN vtiger_stocks AS s ON scf.stocksid = s.stocksid JOIN vtiger_crmentity AS stockent ON stockent.crmid = s.stocksid AND stockent.deleted = 0 LEFT JOIN vtiger_racks AS ra ON ra.racksid = scf.cf_924 JOIN vtiger_rackscf AS racf ON ra.racksid = racf.racksid JOIN vtiger_rooms AS ro ON ro.roomsid = racf.cf_900 JOIN vtiger_roomscf AS rocf ON rocf.roomsid = ro.roomsid JOIN vtiger_warehouses AS w ON rocf.cf_910 = w.warehousesid LEFT JOIN vtiger_vendor AS v ON v.vendorid = p.vendor_id WHERE 1 AND w.warehousesid = 5

SELECT p.productid, p.productname, scf.cf_976 AS stock_quantity, w.warehousename, SUM(IF(scf.cf_974=scf.cf_976 AND scf.cf_974 != 0,1,0)) AS number_of_packs FROM vtiger_products AS p LEFT JOIN vtiger_productcf AS pcf ON pcf.productid = p.productid LEFT JOIN vtiger_stockscf AS scf ON scf.cf_922 = p.productid LEFT JOIN vtiger_stocks AS s ON scf.stocksid = s.stocksid JOIN vtiger_crmentity AS stockent ON stockent.crmid = s.stocksid AND stockent.deleted = 0 LEFT JOIN vtiger_racks AS ra ON ra.racksid = scf.cf_924 JOIN vtiger_rackscf AS racf ON ra.racksid = racf.racksid JOIN vtiger_rooms AS ro ON ro.roomsid = racf.cf_900 JOIN vtiger_roomscf AS rocf ON rocf.roomsid = ro.roomsid JOIN vtiger_warehouses AS w ON rocf.cf_910 = w.warehousesid LEFT JOIN vtiger_vendor AS v ON v.vendorid = p.vendor_id WHERE 1 AND w.warehousesid = 5 AND w.warehousesid = 11324



SELECT p.productid, p.productname, scf.cf_976 AS stock_quantity, w.warehousename, SUM(IF(scf.cf_974=scf.cf_976 AND scf.cf_974 != 0,1,0)) AS number_of_packs FROM vtiger_products AS p LEFT JOIN vtiger_productcf AS pcf ON pcf.productid = p.productid LEFT JOIN vtiger_stockscf AS scf ON scf.cf_922 = p.productid LEFT JOIN vtiger_stocks AS s ON scf.stocksid = s.stocksid JOIN vtiger_crmentity AS stockent ON stockent.crmid = s.stocksid AND stockent.deleted = 0 LEFT JOIN vtiger_racks AS ra ON ra.racksid = scf.cf_924 JOIN vtiger_rackscf AS racf ON ra.racksid = racf.racksid JOIN vtiger_rooms AS ro ON ro.roomsid = racf.cf_900 JOIN vtiger_roomscf AS rocf ON rocf.roomsid = ro.roomsid JOIN vtiger_warehouses AS w ON rocf.cf_910 = w.warehousesid LEFT JOIN vtiger_vendor AS v ON v.vendorid = p.vendor_id WHERE 1 AND w.warehousesid = 5 AND w.warehousesid = 11324 AND w.warehousesid = 35798

我希望得到的结果是仓库编号仅写入一次,并且值从5更改为11324,然后是35798

0 个答案:

没有答案