How do I use regex in a SQLite query?

后端 未结 17 1519
暖寄归人
暖寄归人 2020-11-22 05:57

I\'d like to use a regular expression in sqlite, but I don\'t know how.

My table has got a column with strings like this: \"3,12,13,14,19,28,32\" Now if I type \"whe

17条回答
  •  北海茫月
    2020-11-22 06:22

    SQLite does not contain regular expression functionality by default.

    It defines a REGEXP operator, but this will fail with an error message unless you or your framework define a user function called regexp(). How you do this will depend on your platform.

    If you have a regexp() function defined, you can match an arbitrary integer from a comma-separated list like so:

    ... WHERE your_column REGEXP "\b" || your_integer || "\b";
    

    But really, it looks like you would find things a whole lot easier if you normalised your database structure by replacing those groups within a single column with a separate row for each number in the comma-separated list. Then you could not only use the = operator instead of a regular expression, but also use more powerful relational tools like joins that SQL provides for you.

提交回复
热议问题