0

我的脚本满足要求,但出于这个原因会触发数千个查询,因此需要 50-65 秒。我正在寻找替代的、更优雅和快速的解决方案。

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

这将返回列选项列表,例如 4334、3443、3341、4543

之后,我必须将它与另一列一一比较

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;
}

因此,例如将 4334、3443、3341、4543 与“frombatch”中的 3892、4890、3341、2389 进行比较,将排除 3343。

有没有其他方法可以做到这一点?那么只有 4334、3443、4543 会被返回?

4

2 回答 2

0

似乎您只是想知道 PickBatch 中存在哪些批号,而这些批号在 invoice_web_order 表中任何行的 frombatch 列中的某处尚不存在。

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

您还按客户进行了分组,我不知道为什么。如果您解释,我可以在适当的情况下帮助将其纳入此解决方案。

于 2012-05-09T13:21:52.330 回答
0

你能提供表格定义吗?

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
于 2012-05-09T13:17:58.710 回答