0
range = "e-h"
Term.all(:conditions => ["name[0] = ?", range.split("-")[0]..range.split("-")[1]])

这会产生ActiveRecord::StatementInvalid: 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 '[0] = 'e','f','g','h')' at line 1: SELECT术语.* FROM术语WHERE (name[0] = 'e','f','g','h')

如何从我的术语表中选择名称列的第一个字母在一系列字母之间?

谢谢。

4

1 回答 1

1

这是一个工作示例:

range = ('e'..'h')
Term.where("LEFT(name, 1) IN(?)", range.to_a)

以供参考:

('e'..'h')
左(名称,1)

于 2013-08-27T19:03:37.853 回答