php MySQL在字符串之间获得差异

时间:2012-05-09 13:03:12

标签: php mysql

我的脚本满足要求,但由于这个原因会触发数千个查询,因此需要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

这将返回列434,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?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

你也按照CUSTOMER进行了分组,我不知道为什么。如果您解释,我可以帮助您在适当的时候将其用于此解决方案。