【问题标题】:different result update and select using join in mysql不同的结果更新和选择在mysql中使用join
【发布时间】:2015-03-01 08:34:39
【问题描述】:

我尝试使用以下 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 = ''

它给了我零 (0) 记录结果

虽然我尝试使用类似的 sql 语法执行 select 语句

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 中也与自身连接。您能否验证并最终更新问题? (简而言之,您尝试根据product_class1 更新并仅从product_class2 中选择)
  • 对于更新,您在 product_class1 上进行自我加入,在选择中,您在 product_class2 上进行自我加入。在您引用不同表的两个查询中。
  • 是的,对不起我的错误

标签: mysql sql syntax


【解决方案1】:

您在 update 查询中有错字 - 您将加入 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 = ''

【讨论】:

  • 其实update语句应该使用product_class2,和select语句一样,是可以的。
  • @Frans 参数。更新 - 我误读了选择。好尴尬:-(
猜你喜欢
  • 2016-06-09
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2012-05-29
相关资源
最近更新 更多