如何用符号 id 写请求 sql
How to write request sql with symbol id
我想在 table users
中找到一个 ID 为 :
的用户
<%= @user = User.find_by_sql("SELECT users . * FROM users WHERE id = [:id] ")%>
但我有一个例外:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[:id]' at line 1: SELECT users . * FROM users WHERE id = [:id]
试试这个
<%= @user = User.find_by_sql("SELECT users . * FROM users WHERE id = "[:id]
)%>
find_by_sql(sql, binds = [])
用数组中的绑定包装查询:
=> User.find_by_sql(["select users.email from users where email = :q and first_name = :a", {q: 'foo', a: 'bar'}])
我想在 table users
中找到一个 ID 为 :
<%= @user = User.find_by_sql("SELECT users . * FROM users WHERE id = [:id] ")%>
但我有一个例外:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[:id]' at line 1: SELECT users . * FROM users WHERE id = [:id]
试试这个
<%= @user = User.find_by_sql("SELECT users . * FROM users WHERE id = "[:id]
)%>
find_by_sql(sql, binds = [])
用数组中的绑定包装查询:
=> User.find_by_sql(["select users.email from users where email = :q and first_name = :a", {q: 'foo', a: 'bar'}])