2016-07-28 58 views
0

我想基於以下查詢在MySQL中設置列值。基於連接和自我連接設置列值MySql Rails

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' 

添加到年底,我收到只是說與你的MySQL版本You've got a syntax error near set t1.column_k....速查手冊錯誤。

我真的很想知道如何在這個查詢的結果中包含一個集合,並且在解決這個問題時遇到了麻煩。任何幫助或想法?

對我來說很困難和困惑,我認爲是因爲self join。最終的計劃是將這個帶有set命令的查詢移到一個migration file in rails,一旦我有了它的工作。

回答

0

您需要一個updateSelect不用於設置值。

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';