MYSQL更新语句 - 未知列

时间:2011-10-05 21:36:37

标签: mysql join

我有一个选择查询,可以正常使用以下输出:

enter image description here

我想创建一个更新语句来设置despgoods_alldetails.loadid column = loaddetails.loadid的值,其中despgoods_alldetails_despgoodsid = loaddetails.despgoodsid.loadid。

我正在思考

  

更新despgoods_alldetails   set despgoods_alldetails.loadid = loaddetails.loadid   其中despgoods_alldetails.despgoodsid = loaddetails.despgoodsid

此查询失败,并显示以下错误: enter image description here

任何想法如何让这个更新声明起作用?

谢谢, 莱恩

4 个答案:

答案 0 :(得分:1)

loaddetails是与您要更新的表格不同的表格。这不能直接在SQL更新中完成。你需要一个像这样的子选择:

update despgoods_alldetails
set despgoods_alldetails.loadid = (
    select loaddetails.loadid
    from loaddetails
    where despgoods_alldetails.despgoodsid=loaddetails.despgoodsid
)

答案 1 :(得分:1)

几乎拥有它,你只需要引用第二个表。

UPDATE despgoods_alldetails, loaddetails
    SET despgoods_alldetails.loadid = loaddetails.loadid
    WHERE despgoods_alldetails.despgoodsid=loaddetails.despgoodsid

答案 2 :(得分:1)

您需要在update子句之后放置两个表,如此

update despgoods_alldetails, loaddetails set despgoods_alldetails.loadid =     loaddetails.loadid where despgoods_alldetails.despgoodsid=loaddetails.despgoodsid

您是否可以在此页http://dev.mysql.com/doc/refman/5.0/en/update.html

中找到一个简单示例
UPDATE items,month SET items.price=month.price WHERE items.id=month.id;

答案 3 :(得分:0)

update  
  despgoods_alldetails as da
join 
  loaddetails as l
using 
  (despgoodsid)
set 
  da.loadid = l.loadid;