使用mysql中另一个表的值更新表中的值

时间:2012-05-13 19:48:38

标签: mysql

我的tbltransfer包含locationeq_id。我想更改locationtblequipments的值location tbltransfer,其中eq_id tblequipments等于eq_id tbltransfer中的UPDATE tblequipments SET LocationName = ( SELECT t.Location from tbltransfer as t join tblequipments as e on t.eq_ID = e.eq_ID WHERE t.transactionID=%s ) 。这是我提出的查询:

{{1}}

2 个答案:

答案 0 :(得分:1)

mysql将为update语句中的每一行运行子查询。最好在这里使用join:

UPDATE
    tblequipments,
    tbltransfer
SET
    tblequipments.location = tbltransfer.location
WHERE
    tblequipments.eq_id = tbltransfer.eq_id
AND
    tbltransfer.transactionID = %s

答案 1 :(得分:1)

UPDATE tblequipments
INNER JOIN tbltransfer ON tblequipments.eq_ID = tbltransfer.eq_ID 
SET tblequipments.LocationName=tbltransfer.Location
WHERE tbltransfer.transactionID=%s