0

I posted a question regarding this script/query the other day, but it was regarding strictly being able to insert values, not including new logic.

I currently have a PHP script that 'works' in terms of selecting and inserting but I need to apply some logical modifications to it in order to do a bit more.

I have a DB2 select query:

//DB2 Select query
$plcQueryDB2 = " 

        select  
            invnoc as INVOICE,
            cstnoc AS DEALER,
            framec AS FRAME,
            covr1c AS COVER,
            colr1c AS COLOR ,
            extd1d AS SHIPDATE,
            orqtyc AS QUANTITY
        FROM GPORPCFL
        where extd1d >= curdate()
        group by invnoc,cstnoc, orqtyc, framec, covr1c,colr1c, extd1d
        order by invnoc asc
";

Then I basically prepare and exucute that, and loop it in order to do a PDO call to insert into mysql. I'm actually using some variables from the DB2 query to 'join' with some MySQL data in another table, and then taking all the data from that and inserting into a final Mysql table.

Again, this works currently. However, I need to apply some additional logic and looping around my insert so that I can determine whether a record is updated or inserted.

I have some commented lines in my while loop below to give a pseudo code approach to what I need to do, and it includes the working insert. With the following points, I'm assuming a hypothetical unique index/combo of sku_id and dealer_id Basically, I need to address these points:

  1. Within the while loop, query the mysql 'placements' table first to see if a record exists for the combo of sku_id and dealer. (So I would have to iterate over each held record from my db2 select and check to see if any records exist for that sku/dealer combo exists)
  2. If there are no records for that sku_dealer combo, then I will only do an insert. However, I need to insert a number of records equal to the value of 'QUANTITY' from the DB2 query. So insert the same record (I know this is redundant, but there's a reason for it) n times until n = QUANTITY
  3. If records exists with the sku/dealer combo AND today/curdate() is before expire_date of those records, then update those existing records, still making sure the number of rows/records = the value of 'QUANTITY' from above.
  4. Else, if records exist but today/curdate() is past the expiration, then do the insert from step 2 ensuring row count = QUANTITY

I think that should clear it up but maybe the pseudo code in my loop below will help clarify that.

I just need some guidance here, and maybe I've been looking at it for too long since I got it to this point. But what is a good way to apply new loops and logic to my existing code to achieve those points or at least get on the right path? This will run daily and I'm NOT concerned with efficiency at all right now, I just need to get it working and I can address speed or performance later.

Any ideas, suggestions or guidance will be much appreciated.

Here's the rest of the script:

//prepare and execute on db2
$prep = odbc_prepare($DB2Conn, $plcQueryDB2);
$exec = odbc_execute($prep);
$result = odbc_exec($DB2Conn, $plcQueryDB2);

try{

    while($arr = odbc_fetch_array($result)) 
    {

        //loop and query the placements table to see if a record exists for the sku_id/group_id/dealer_ID


        //if it exists and curdate() < expire_date and row_count = locations

            // update starting with oldest expire_date

        //else if row_count < locations

        $stmt = $PDO->prepare("
            INSERT IGNORE INTO placements (sku_id, group_id, dealer_id, start_date, expire_date, locations, order_num)
                SELECT 
                    id, 
                    sku_group_id, 
                    :DEALER, 
                    DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 7 DAY) as start_date,
                    DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date, 
                    :QUANTITY,
                    :INVOICE  
                FROM skus s
                    WHERE  s.frame=:FRAME AND s.cover1=:COVER AND s.color1=:COLOR 
        ");

        $PDO->beginTransaction();
        $i = 0;
        while($db2row = odbc_fetch_array($result)) {

            if(++$i % 1000 == 0) {
                $PDO->commit();
                $PDO->beginTransaction();
            }
            $stmt->execute($db2row);
        }
        $PDO->commit();
    }
}catch (PDOException $e){
    print "ERROR!: " . $e->getMessage() . "<br/>";
    die();
}

This may also help with the logic behind the update/insert:

If trying to insert this record:

sku_id   |   group_id   |   dealer_id   |   start_date   |   expire_date      |   locations
-------------------------------------------------------------------------------------------
123         10              15              2018-02-02     (start + 127 days)      3

If there are already any records for the same sku/group/dealer combo (123/10/15) AND the current date is before the expire_date for those records, we will be updating.

If the expire_date for those records has already passed, we will be inserting 3 new records (because locations has a value of 3)

So for the following records:

sku_id   |  dealer_id   |   locations
--------------------------------------
123         10              3
123         10              3
123         10              3

The logic would say "There are already records with the hypothetical unique index of sku_id/group_id/dealer_id (123/10/15) and the expire date has passed, so insert 3 records for

sku_id   |   group_id   |   dealer_id   |   start_date   |   expire_date      |   locations
-------------------------------------------------------------------------------------------
123         10              15              2018-02-02     (start + 127 days)      3

Otherwise, if those 3 records above had an expire_date of 2018-05-01, then we would update each row with the new expire_date (start_date + 127 days)

8
  • Very odd GROUP BY... Commented Feb 28, 2018 at 14:04
  • Possible duplicate of Inserting into mysql table based on results from a db2 query in php script Commented Feb 28, 2018 at 14:11
  • 1
    May be instead of posting the same question over and over again you could spend some time trying to make it clearer and easier to answer? Commented Feb 28, 2018 at 14:12
  • @mustaccio I deleted my other questions related to this issue. And the one you marked as duplicate addressed the issue of the insert, but has nothing to do with applying this logic. Again, I have a standard insert working but I'm trying to get some guidance on the new logic involved. The one you linked to has an answer that I accepted. This question is much more concise and clear than what I posted yesterday Commented Feb 28, 2018 at 14:18
  • @jarlh We have RPG developers that work with the DB2 data and they created a version of that query with the same group by, strictly to eliminate duplicates because of how many tables and relationships we have involving products Commented Feb 28, 2018 at 14:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.