我需要提取first_name
列的值以非標準英文字母(即A-Z)開頭的結果子集,例如È
。我敲我的頭撞牆本 - 例如,這是怎麼了,我已經能夠得到以字母A
的所有記錄:查找first_name列未以A-Z開頭的所有記錄
@results = User.where("first_name LIKE ?", "A%").order("first_name ASC")
任何幫助將是美好的。
編輯:我正在使用PostgreSQL數據庫。
我需要提取first_name
列的值以非標準英文字母(即A-Z)開頭的結果子集,例如È
。我敲我的頭撞牆本 - 例如,這是怎麼了,我已經能夠得到以字母A
的所有記錄:查找first_name列未以A-Z開頭的所有記錄
@results = User.where("first_name LIKE ?", "A%").order("first_name ASC")
任何幫助將是美好的。
編輯:我正在使用PostgreSQL數據庫。
您可以使用~
運算符來匹配正則表達式。 ~
用於匹配case sensitive
數據。所以在你的情況下,first_name ~ '^[^A-Z]'
將給users
其0123¾不以A-Z
開頭。要匹配case insensitive
,您需要使用~*
。
@results = User.where("first_name ~ ?", "^[^A-Z]").order("first_name ASC")
或
也可以使用!~
其給出不匹配的結果(即)first_name !~ ^[A-Z]
相當於first_name ~ ^[^A-Z]
。匹配case insensitive
使用!~*
。
@results = User.where("first_name !~ ?", "^[A-Z]").order("first_name ASC")
欲瞭解更多信息,請參閱documentation
謝謝你,這工作完美,我一直對流感感到抱歉,它花了我很長時間纔得到這個答覆,它真的幫了我。 – 2013-03-01 11:04:13
你能提供你的數據庫系統名嗎? MySQL的? PostgreSQL的? – MrYoshiji 2013-02-22 18:12:39
對不起PostGreSQL – 2013-02-22 18:13:07