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表中选择名称列的第一个字母在一系列字母之间?
感谢。
答案 0 :(得分:1)
这是一个有效的例子:
range = ('e'..'h')
Term.where("LEFT(name, 1) IN(?)", range.to_a)
供参考: