-1

我想用另一个表两行的内容更新一个表的行。有没有比使用 2 别名 ( ) 锁定表更有效的方法new_data AS x & y

SET AUTOCOMMIT=0;

START TRANSACTION;

LOCK TABLES
1_products_speech WRITE,
new_data as x WRITE,
new_data as y WRITE;

UPDATE 1_products_speech 
SET 
    v1 = (SELECT 
            data_blob
        FROM
            new_data as x
        WHERE
            id = 1),
    v2 = (SELECT 
            data_blob
        FROM
            new_data as y
        WHERE
            id = 2)
WHERE
    fk_products_id = 1890;

COMMIT;

UNLOCK TABLES;
4

1 回答 1

1

Mysql 支持多表更新语句,因此您可以将查询重写为:

UPDATE 1_products_speech 
JOIN new_data x ON id = 1
JOIN new_data y ON id = 2
SET
  v1 = x.data_blob,
  v2 = y.data_blob
WHERE fk_products_id = 1890;

因为与其他表的连接不依赖于主表中的值,所以我怀疑它在性能方面是否更有效,尽管它在编码方面可能被认为更有效。

于 2013-05-16T11:03:08.297 回答