2014-04-22 56 views
0

我想更新從一個表到另一個行,兩者具有相同的結構,我用這個查詢如何從另一個表中更新一個表?

UPDATE bh_master_ritesh_stock AS st 
SET st.tag_uid = apprv.tag_uid ,st.stk_id = apprv.stk_id 
FROM bh_stock_approval AS apprv 
WHERE st.stock_id = apprv.stock_id 

,但它給了我這個錯誤,

You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near 
'FROM bh_stock_approval as apprv WHERE st.stock_id = apprv.stock_id' at line 3 

哪裏是我的錯?或者我使用錯誤的查詢?

回答

1

試試這個代碼:

UPDATE bh_master_ritesh_stock AS st,bh_stock_approval AS apprv 
SET st.tag_uid = apprv.tag_uid ,st.stk_id = apprv.stk_id 
WHERE st.stock_id = apprv.stock_id 

因爲UPDATE不能使用From條款

+0

你還是使用連接的可讀性 –

+0

的問題是在哪裏錯誤..如果問題是如何有效地查詢是應該而使用連接:) –

1

你的語法是完全錯誤的 「從」 出乎意料:

UPDATE bh_master_ritesh_stock AS st,bh_stock_approval AS apprv 
SET st.tag_uid = apprv.tag_uid ,st.stk_id = apprv.stk_id 
WHERE st.stock_id = apprv.stock_id 

對於其他的例子,涉及到更多表,看看here

0

試試這個。

UPDATE bh_master_ritesh_stock st 
INNER JOIN bh_stock_approval apprv on 
    st.stock_id = apprv.stock_id 
SET 
    st.tag_uid = apprv.tag_uid ,st.stk_id = apprv.stk_id 
0
UPDATE bh_master_ritesh_stock A 
SET A.tag_uid, A.stk_id =(select B.tag_uid, B.stk_id 
FROM bh_stock_approval B WHERE A.stock_id = B.stock_id) 
WHERE EXISTS(SELECT '1' FROM bh_stock_approval B1 WHERE A.stock_id =B1.stock_id)