在mysql中使用join更新和选择不同的结果

在mysql中使用join更新和选择不同的结果,mysql,sql,syntax,Mysql,Sql,Syntax,我尝试使用以下sql语法更新一些记录 update product_class1 t1 join product_class1 t2 on t1.family_code = t2.family_code set t1.parent_id = t2.id where t1.id < 145 and t1.id > 140 and t2.class_code = '' select * from product_class2 t1 join product_class2 t2

我尝试使用以下sql语法更新一些记录

update product_class1 t1 
join product_class1 t2
on t1.family_code = t2.family_code
set t1.parent_id = t2.id
where t1.id < 145
and t1.id > 140
and t2.class_code = ''
select *
from  product_class2 t1 
join  product_class2 t2
on  t1.family_code = t2.family_code
where t1.id < 145
and t1.id > 140
and t2.class_code = ''
它给我零记录结果

当我尝试使用类似的sql语法执行select语句时

update product_class1 t1 
join product_class1 t2
on t1.family_code = t2.family_code
set t1.parent_id = t2.id
where t1.id < 145
and t1.id > 140
and t2.class_code = ''
select *
from  product_class2 t1 
join  product_class2 t2
on  t1.family_code = t2.family_code
where t1.id < 145
and t1.id > 140
and t2.class_code = ''
它给我4条记录的结果

我不知道我的update sql语句有什么问题。 如果有人能指出这个建议,我将不胜感激

关于

您在更新查询中有一个输入错误-您将product_class1与其自身连接,而不是使用product_class2。只要修改join语句,就可以了

update product_class2 t1 --here!
join product_class2 t2 -- and here!
on t1.family_code = t2.family_code
set t1.parent_id = t2.id
where t1.id < 145
and t1.id > 140
and t2.class_code = ''

我不确定是否是这种情况,但请注意,在更新查询中,您将product_class1与自身连接起来,在选择查询中,product_class2也与自身连接起来。你能核实并最终更新这个问题吗?简而言之,您尝试根据产品类别1进行更新,并仅从产品类别2中进行选择,以便更新您在产品类别1上进行的自连接,在选择中,您在产品类别2上进行的自连接。在这两个查询中,您引用的是不同的表。是的,对不起,我弄错了。事实上,update语句应该使用product_class2,与select语句相同,并且有效。@Frans arg。更新-我误读了选择。多么尴尬啊:-