Sample Header Ad - 728x90

How I can check whether a column contains a substring of a given searchterm?

0 votes
2 answers
449 views
In sqlite I have the following table
CREATE table IF NOT EXISTS redirect (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            url_from TEXT not null,
            url_to TEXT not null,
            method TEXT not null,
            http_status_code INTEGER not null CHECK( http_status_code IN (300,301,302,304,304,308,307) ) DEFAULT 301,
            use_in_https  INTEGER not null CHECK(use_in_https IN (0,1)) DEFAULT 0,
            use_in_http  INTEGER not null CHECK(use_in_http IN (0,1)) DEFAULT 1,
            exact_match INTEGER not null CHECK(exact_match IN (0,1)) DEFAULT 1
        );
And I insert the following values:
INSERT INTO redirect (url_from,url_to,method,http_status_code,use_in_http,use_in_https) VALUES
            ('http://google.com/mytest ','http://yahoo.com ','GET',301,1,1),
            ('http://google.com/mytest?param=hello ','http://ikariam.gr ','GET',301,1,1),
            ('http://google.com/mytest ','http://yandex.com ','GET',302,1,0)
In my app I search based on an incomming url and I want to find the best match: If the url is http://google.com/mytest I want the following result to be returned: url_from | url_to | method | http_status_code | use_in_http | use_in_http --- | --- | --- | --- | --- | --- | http://google.com/mytest ' | 'http://yahoo.com ' |'GET' | 301 | 1 | 1 But once I either for: * http://google.com/mytest/lorem_uipsus * http://google.com/mytest?param=petralka * http://google.com/mytest?param33=petralka I want this result to be returned: url_from | url_to | method | http_status_code | use_in_http | use_in_https --- | --- | --- | --- | --- | --- | http://google.com/mytest ' | 'http://yandex.com ' |'GET' | 302 | 1 | 0 As you can see request_from may contain a subscript for the provided search term. Therefore, I thought the query should be:
select 
  url_from,url_to,method,http_status_code,use_in_http,use_in_https
from
  redirect
where
  -- Here search by substring
  -- url_from must be a substring of a provided search term
order by exact_match DESC
LIMIT 1
But here I am stuck, how I can check that a column contains a substring of a provided searchterm? What I want is to do the opposite of:
url_from like '%input%
Meaning:
'input' like url_from
Any ideas?
Asked by Dimitrios Desyllas (873 rep)
Jan 25, 2023, 09:36 PM
Last activity: Mar 3, 2025, 06:37 AM