0
我的環境是Ruby 1.8.7-p358 with Rails 2.3.11
。我試圖查詢所有accounts
,其中first_name
只包含數字(它們都是3位整數,即001,143,815等)。我一直在嘗試使用此查詢:查詢唯一整數Rails 2.3
Account.find(:all, :conditions => ["first_name LIKE ?", "[^0-9][^0-9][^0-9]"])
我所得到的回報是=> []
。
如果我僅使用與NOT LIKE
相同的查詢,我會得到所有accounts
,其中first_name
是一個整數。
我也嘗試使用:
Account.find(:all, :conditions => ["first_name REGEXP ?", "[^0-9][^0-9][^0-9]"])
,但只給了我:
ActiveRecord::StatementInvalid: PGError: ERROR: syntax error at or near "REGEXP"
LINE 1: SELECT * FROM "accounts" WHERE (first_name REGEXP '[0-9][0-9...
^
: SELECT * FROM "accounts" WHERE (first_name REGEXP '[0-9][0-9][0-9]') ORDER BY first_name ASC
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/connection_adapters/abstract_adapter.rb:227:in `log'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/connection_adapters/postgresql_adapter.rb:520:in `execute'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1002:in `select_raw'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/connection_adapters/postgresql_adapter.rb:989:in `select'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/base.rb:665:in `find_by_sql'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/base.rb:1582:in `find_every'
from /Users/kyle/.rvm/gems/[email protected]_recruiter_portal/gems/activerecord-2.3.11/lib/active_record/base.rb:619:in `find'
from (irb):189
from :0
是正則表達式不允許?我怎樣才能找到每個account
與first_name
等於一個三位數的整數?
Arrghh!仍然清空! '>> a = Account.find(:all,:conditions => [「first_name〜?」,「[0-9] [0-9] [0-9]」]) => [] >> a = Account.find(:all,:conditions => [「first_name SIMILAR TO?」,「[0-9] [0-9] [0-9]」]) => []' –
@JustLikeThat There是你的「[0-9] [0-9] [0-9]」 –
的領先空間。你是對的!運營商「SIMILAR TO」運作良好!謝謝! –