我正在尝试根据以下查询在 MySQL 中设置列值。
select * from table1 t1
join table2 t2
on t1.id = u.t1_id
and t2.status = 'verified'
and not exists (
select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id
and t2_2.updated_at > t2.updated_at
)
此查询返回我想要的结果,但是当我尝试添加
SET t1.column_k = 'some value'
到最后,我收到一个错误,简单地说You've got a有一个语法错误在set t1.column_k....
检查与您的MySQL版本相对应的手册
.
我真的很想知道如何在此查询的结果中包含一个集合,但在制定这一点时遇到了困难。有什么帮助或想法吗?
我认为这对我来说很困难且令人困惑,因为自加入
。最终的计划是,一旦我让它工作起来,就将此查询与 set 命令移植到 Rails 中的迁移文件中。
最佳答案
您需要更新
。 Select
不用于设置值。
update table1 t1 join
table2 t2
on t1.id = u.t1_id and
t2.status = 'verified' and
not exists (select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id and
t2_2.updated_at > t2.updated_at
)
set t1.column_k = 'some value';
关于mysql - 基于联接和自联接设置列值 MySql Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38625588/