2014-11-05 46 views
-1

這個MySQL查詢拒不歸還的結果出沒:這個SELECT查詢有什麼問題?沒有錯誤,沒有結果

select entity_id 
from customer_entity 
where entity_id NOT IN (SELECT sfo.customer_id 
         FROM sales_flat_order sfo); 

如果分割兩種說法,他們獨立工作:

select entity_id from customer_entity; 

將返回... 1,2 ,3,4,5,6

select sfo.customer_id FROM sales_flat_order sfo; 

將retur n .... 1,1,2,5,6

返回相同的數據類型 - INTEGER(10) - 和許多相同的值。

我假設問題可能與兩列沒有相同的名稱有關,但我不確定在這一點上。

更多信息:確實有很多空訂單。 sales_flat_order表只包含45天的數據,而customer_entity表包含5年或6年的數據。我試圖將customer_entity表縮小到45天。選擇查詢只是我將運行的刪除查詢的前兆。

+0

你可以包括一些示例數據和表描述? – 2014-11-05 20:08:34

+1

您的customer_entity表中的所有記錄似乎都有一個entity_id,它存在於sales_flat_order表中的customer_id中。作爲輸出你想達到什麼目的? – Dan 2014-11-05 20:11:08

+0

您是否嘗試過不存在? – aa333 2014-11-05 20:14:47

回答

1

假設有記錄customer_entity那些在sales_flat_order沒有相關的記錄,這應該是你正在尋找:

select 
    entity_id 
from 
    customer_entity 
where 
    not exists (SELECT 
        1 
       FROM 
        sales_flat_order sfo 
       WHERE 
        sfo.customer_id = entity_id); 
-2

編寫查詢的另一種方法是使用左連接運算符。

SELECT entity_id 
FROM customer_entity 
LEFT JOIN sales_flat_order sfo 
ON entity_id = sfo.customer_id 
WHERE entity_id IS NULL 
+0

'sfo'未定義 – hjpotter92 2014-11-05 20:22:01