Joe*_*oey 1 regex sql database postgresql
几天来,我一直在尝试解决以下问题。请帮忙
PostgreSQL 表:位置
Id State
--------------------
1 New York
2 Texas
Run Code Online (Sandbox Code Playgroud)
input = '来自德克萨斯州的问候所有牛仔'
输出:包含德克萨斯的行
SELECT id, state FROM locations WHERE state ~* substring(input from state)
1.
select * from locations where 'Greetings from Texas to all Cowboys' ~ State;
Run Code Online (Sandbox Code Playgroud)
2.
select * from locations where State = any(string_to_array('Greetings from Texas to all Cowboys',' '));
Run Code Online (Sandbox Code Playgroud)
以上两种方法在某些情况下都有一些问题,但我想知道它们是否适合您。
3.
select * from locations where 'reetings from Texas to all Cowboys' ~* ('\\m' || state || '\\M');
Run Code Online (Sandbox Code Playgroud)
最后一种方法会更好。