如何从另一个表更新一个表?

时间:2014-04-22 07:32:49

标签: mysql

我想将行从一个表更新到另一个表,两者都具有相同的结构,我使用此查询

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

我的错误在哪里?或者我使用了错误的查询?

4 个答案:

答案 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

因为UPDATE无法使用From子句

答案 1 :(得分:1)

您的语法完全错误,“FROM”是意外的:

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

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

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