简体   繁体   English

使用来自两个select语句的绑定参数,并在一个插入中使用它们

[英]Using bound parameters from two select statements and use them in one insert

I'm trying to figure out how to take bound parameter values from two different select statements and insert them into a new table. 我试图弄清楚如何从两个不同的select语句中获取绑定参数值并将其插入到新表中。

My first select gets an array without issue. 我的第一选择得到一个没有问题的数组。 My second select gets a count, as well as a number 180 divided by the count, but uses returned values from the first select in it's where clause. 我的第二个选择得到一个计数,以及一个除以该计数的数字180,但使用了where子句中第一个选择的返回值。

This all works perfect. 这一切都完美。

Now, based on each of the 2nd select's execution I want to insert the values from each select into one new table. 现在,基于第二个选择的执行,我想将每个选择的值插入一个新表中。

I'm binding the values from each select individually, but how can I bind the values from both selects to execute my insert on them? 我要分别绑定每个选择的值,但是如何绑定两个选择的值以对它们执行插入?

$selectPLC = "
    SELECT 
        sku_id, s.frm as frm, sg.code as code, s.matrl as matrl, s.color as color, cst
    FROM plc;
";

try {
    $PLCcheck = $MysqlConn->prepare($selectPLC);
    $detailRslt = $PLCcheck->execute();

     while ($PLCRow = $PLCcheck->fetch(PDO::FETCH_ASSOC)) {

        print_r($PLCRow); //This prints the first array I need

        $salesValues = [
        ":cst" => $PLCRow["cst"],
        ":frm" => $PLCRow["frm"],
        ":matrl" => $PLCRow["matrl"],
        ":color" => $PLCRow["color"]

        ];

        $checkSales = "
            SELECT
            count(*) as salesCount,
            180/count(*) as countDIV
            FROM orders
                WHERE cstnoc = :cst
                AND frmc = :frm
                AND covr1c = :matrl
                AND colr1c = :color
                AND date(substr(dateField1,1,4)||'-'||substr(dateField1,5,2)||'-'||substr(dateField1,7,2) ) between current_Date - 180 DAY AND current_Date
        ";

        try{
            $salesCheck = $DB2Conn->prepare($checkSales);
            $salesResult = $salesCheck->execute($salesValues);

            while ($salesRow = $salesCheck->fetch(PDO::FETCH_ASSOC)) {

                print_r($salesRow); //This prints the 2nd array I need

                $countValues = [
                    ":salesCount" => $salesRow["salesCount"],
                    ":countDiv" => $salesRow["countDiv"]
                ];

                $insertSales = "

                    INSERT INTO metrics (cst, frm, matrl, color, salesCount, countDIV )
                    VALUES (
                        :cst, //from first array
                        :frm, //from first array
                        :matrl, //from first array
                        :color, //from first array
                        :salesCount, //from 2nd array
                        :countDiv //from 2nd array
                    )
                ";

                $salesInsertPrep = $DB2Conn->prepare($insertSales);
                $salesInsertExec = $salesInsertPrep->execute($countValues);
            }

        }catch(PDOException $ex) {
            echo "QUERY TWO FAILED!: " .$ex->getMessage(); 
        }
    }

}catch(PDOException $ex) {
    echo "QUERY ONE FAILED!: " .$ex->getMessage();
}

When your fetching the second array set of values, rather than add them to a new array, you can add them into the array of the first values, so instead of... 当获取第二个数组值而不是将它们添加到新数组中时,可以将它们添加到第一个值数组中,因此...

       $countValues = [
            ":salesCount" => $salesRow["salesCount"],
            ":countDiv" => $salesRow["countDiv"]
        ];

use 采用

$salesValues[":salesCount"] = $salesRow["salesCount"];
$salesValues[":countDiv"] = $salesRow["countDiv"];

and then... 接着...

$salesInsertExec = $salesInsertPrep->execute($salesValues);

The other thing is that you can prepare your insert once outside the loop and then execute it each time in the loop, so this would look like... 另一件事是,您可以在循环外准备一次插入,然后在循环中每次执行插入,因此看起来就像...

    $insertSales = "
                INSERT INTO metrics (cst, frm, matrl, color, salesCount, countDIV )
                VALUES (
                    :cst, //from first array
                    :frm, //from first array
                    :matrl, //from first array
                    :color, //from first array
                    :salesCount, //from 2nd array
                    :countDiv //from 2nd array
                )
            ";

    $salesInsertPrep = $DB2Conn->prepare($insertSales);
    while ($salesRow = $salesCheck->fetch(PDO::FETCH_ASSOC)) {

        print_r($salesRow); //This prints the 2nd array I need

        $salesValues[":salesCount"] = $salesRow["salesCount"];
        $salesValues[":countDiv"] = $salesRow["countDiv"];
        $salesInsertExec = $salesInsertPrep->execute($salesValues);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM