【问题标题】:Can you do a where find with a regular expression in rails?你能用rails中的正则表达式做一个where find吗?
【发布时间】:2012-03-08 13:56:08
【问题描述】:

我正在尝试查找以数字开头的对象。

从语法上讲,这是关闭的。但我想做一些类似的事情:

Object.where([name LIKE ?', /[1-9]/])

如果不可能,您认为找到所有以数字开头的对象的最佳方法是什么?

【问题讨论】:

    标签: mysql ruby-on-rails activerecord


    【解决方案1】:

    Select values that begin with a number

    Object.where(['name REGEXP ?', '^[0-9]'])
    

    【讨论】:

      【解决方案2】:

      我认为你可以使用 rlike / regexp。虽然它不能移植到其他数据库。

      http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp

      Object.where(['name rlike ?', '^[\d]'])
      

      【讨论】:

      • 嗯,这没用,但是where(['name rlike ?', '^[1-9]'])
      猜你喜欢
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 2017-03-03
      • 2012-02-02
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多