Rails SQL regular expression

后端 未结 3 937
粉色の甜心
粉色の甜心 2020-12-28 12:39

I\'m trying to search for the maximum number in the series A0001, A0002, A1234, A2351, etc... The problem is that the list I\'m searching in also has strings such as AG1089

3条回答
  •  感动是毒
    2020-12-28 13:02

    You did a good job! The thing missing was the REGEXP function which is used for regex in queries:

    So in your case use

    Drawing.where("drawing_number REGEXP ?", 'A\d{4}')
    # the {4} defines that there have to be exactly 4 numbers, change if you need to
    

    In SQL you use the '-colons, which is weird because you normally start regex with /-backslashes

提交回复
热议问题