mysql语法更新记录基于其他记录相同的表

时间:2015-01-02 16:04:08

标签: mysql sql

如何在mysql上创建sql语法, 当我想基于特定列字段上具有相同值的同一表上的另一条记录上的id更新parent_id时,示例字段代码

我试图制作以下内容

update product_class t1
set t1.parent_id = t2.id
WHERE t1.family_code <>'' and t1.class_code = ''
join product_class t2
on 
(t1.segment_code = t2.segment_code)

但是给了我错误

这是表结构: table schema

1 个答案:

答案 0 :(得分:1)

这是正确的语法:

update product_class t1 join
       product_class t2
       on t1.segment_code = t2.segment_code
    set t1.parent_id = t2.id
    where t1.family_code <> '' and t1.class_code = '';

join是MySQL中update子句的一部分。

注意:查询看起来不会做正确的事情。您正在对看起来像非唯一列的自我连接进行自我连接,这将生成大量匹配项。然后,update将使用任意匹配的行。

相关问题