作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Rails 应用程序中测试以下代码:
a = Ex.all.pluck(:id)
b = Ex.where("name LIKE ?", "%asdf%").where("id IN ?", a).pluck(:id)
它正在生成这个 PostgreSQL 语句:
SELECT "exs"."id" FROM "exs" WHERE (name LIKE '%asdf%') AND (id IN 9,10,11,12,13,14,15)
但是报错:
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec':
PG::SyntaxError: ERROR: syntax error at or near "9" (ActiveRecord::StatementInvalid)
LINE 1: "exs" WHERE (name LIKE '%asdf%') AND (id IN 9,10,11,12...
^
出了什么问题?这是我第一次使用这种查询,我一直无法在任何指南或类似问题中找到答案。
感谢您的宝贵时间。
最佳答案
使用IN
时,需要将选项括在括号中:
.where("id IN (?)", a)
关于postgresql - 使用 IN 的 Rails PostgreSQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36411635/
我是一名优秀的程序员,十分优秀!