2012-05-09 77 views
0

我的腳本滿足要求,但因爲這個原因觸發了幾千個查詢,所以需要50-65秒。我正在尋找替代,更優雅和快速的解決方案。php MySQL獲取字符串之間的差異

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

之後,我必須把它逐個對比4334,3443的另一列

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

因此,例如比較例如,來自「frombatch」的3892,4890,3341,2389將排除3343.

有沒有其他方法可以做同樣的事情?那麼只有4334,3443,4543會被退回?

+2

當你說「將排除3343」。你的意思是「會排除3341」嗎? –

回答

0

好像你只是想知道這PickBatch不已經某處frombatch列在表invoice_web_order中存在的任何行存在批號。

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分組,我不知道爲什麼。如果你解釋一下,我可以在適當的情況下幫助將它應用於這個解決方案。

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