根据列上的字母范围从集合中进行选择

时间:2013-08-27 05:02:16

标签: sql ruby-on-rails-3 activerecord

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

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

感谢。

1 个答案:

答案 0 :(得分:1)

这是一个有效的例子:

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

供参考:

('e'..'h')
LEFT(name, 1)