2017-10-17 184 views
0

我試圖運行下面的代碼我的SQL更新犯規犯

update apidura_dwh.B2C_order_line 
join apidura_dwh.B2C_orders 
using (order_id) 
set apidura_dwh.B2C_order_line.order_date = apidura_dwh.B2C_orders.order_date; 
commit; 

基本上只是想增加訂單日期到行表 - 這將返回一個成功的消息,並說,行已經被改變 - 但是當我查詢訂單行表時,沒有行被更改。

enter image description here

它似乎工作,如果我Orders表

enter image description here

  • 如何使這項工作任何想法加入?
+0

您使用的是哪種SQL客戶端?它有一個提交按鈕? – wast

+0

我會避免在連接中使用using子句,除非您100%確定(以及爲什麼依賴該子句),即每個表中只有1列可以使用。 –

回答

0

這會建議您有不匹配的訂單或具有NULL日期的訂單。首先,我會寫查詢爲:

update apidura_dwh.B2C_order_line ol join 
     apidura_dwh.B2C_orders o 
     using (order_id) 
    set ol.order_date = o.order_date; 
commit; 

要測試更新工程 - 並假設ol.order_idNULL - 那麼你可以做:

select count(*) 
from apidura_dwh.B2C_order_line ol 
where ol.order_date is null; 

前和更新後。

你可以看看這樣做缺少訂單ID:

select count(*) 
from apidura_dwh.B2C_order_line ol left join 
    apidura_dwh.B2C_orders o 
    using (order_id) 
where o.order_id is null; 

而對於使用NULL日期:

select count(*) 
from apidura_dwh.B2C_orders o 
where o.order_date is null; 
+0

謝謝你的答案 - 我發現沒有缺少日期或訂單ID(在B2C訂單表中)的訂單 - 問題出現在訂單行表中,我無法使用更新填充order_date列。感謝 –

0

不能使用JOIN一個UPDATE查詢中。

UPDATE B2C_order_line 
SET B2C_order_line.order_date = B2C_orders.order_date 
FROM B2C_orders 
WHERE 
B2C_order_line.order_id = B2C_orders.order_id; 

試試這個。

+0

謝謝 - 我試過了,但它給了我一個錯誤 - 我做了如下改變 UPDATE B2C_order_line SET B2C_order_line.order_date =(選擇B2C_orders.order_date FROM B2C_orders WHERE B2C_order_line.order_id = B2C_orders.order_id);然而,這只是有同樣的問題 –