如何使用RubyonRails来实现这一点?
mysql> SET @t1=1, @t2=2, @t3:=4; mysql> SELECT @t1, @t2, @t3, @t4 := @t1+@t2+@t3;
使用 ActiveRecord::Base.connection :
ActiveRecord::Base.connection
>> ActiveRecord::Base.connection.execute("SET @t1=1, @t2=2, @t3:=4;") => nil >> ActiveRecord::Base.connection.select_one( "SELECT @t1, @t2, @t3, @t4 := @t1+@t2+@t3;") => {"@t1"=>"1", "@t2"=>"2", "@t4 := @t1+@t2+@t3"=>"7", "@t3"=>"4"}
有关可以在此类上使用的更多方法,请参见 the documentation here .
您也可以拨打 connection 方法对您定义的任何ActiveRecord类。例如,如果您有一个名为 Post ,你可以使用 Post.connection.execute("sql") .
connection
Post
Post.connection.execute("sql")