简体   繁体   中英

php MySQL getting difference between strings

My script fulfills the requirement but fires several thousand of queries for this reason, so it take 50-65 seconds. I am looking for alternative, more elegant and quick solution.

SELECT GROUP_CONCAT(DISTINCT BatchNo SEPARATOR ', ') AS picklists, web_company_name
FROM PickBatch 
LEFT JOIN customer_master ON customer_master.VC_CUSTOMER_CODE = PickBatch.Customer
GROUP BY Customer

This returns column Picklists like 4334, 3443, 3341, 4543

After that I have to compare it one by one with another column

while( $row=mysql_fetch_array($res) )
{   
     $picklists = explode(', ', $row['picklists']);
     $pickString = '';
     foreach($picklists as $batch)
     {
         //invoiced
         $sql2 = "SELECT invoice_no FROM invoice_web_order WHERE FIND_IN_SET('$batch', frombatch) LIMIT 1";
         $res2 = mysql_query($sql2) or die(mysql_error());
         if ( mysql_num_rows($res2) > 0 )
         {
             continue;
         }   
        $pickString .= "$batch, ";
    }
    echo $pickString;
}

So for example comparing 4334, 3443, 3341, 4543 with, for instance, 3892, 4890, 3341, 2389 from "frombatch" will exclude 3343.

Are there any other way to do the same? So that only 4334, 3443, 4543 will be returned?

Could you please provide table definition?

SELECT 
    DISTINCT BatchNo as BN, 
    GROUP_CONCAT(DISTINCT BatchNo SEPARATOR ', ') AS picklists, 
    web_company_name
FROM PickBatch
LEFT JOIN customer_master ON customer_master.VC_CUSTOMER_CODE = PickBatch.Customer
WHERE PickBatch.BN <>      (SELECT frombatch 
                            FROM invoice_web_order 
                            WHERE FIND_IN_SET(PickBatch.BN, frombatch)
                            LIMIT 1)
GROUP BY Customer

It seems like you just want to know which Batch numbers exist in PickBatch that doesn't already exist somewhere in the frombatch column in any row in the invoice_web_order table.

Table: PickBatch
BatchNo
4334
3443
3341
5453

Table: invoice_web_order
frombatch
3982, 4890, 3341, 2389
####, ####, ####
####, ####, ####, ####

SELECT DISTINCT batchno FROM PickBatch LEFT JOIN invoice_web_order ON FIND_IN_SET(batchno, frombatch) WHERE invoice_web_order.frombatch IS NULL

You also did a grouped by CUSTOMER and I'm not sure why. If you explain, I can help with work it into this solution if appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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