How do you match multiple column in a table with S

2019-01-24 02:26发布

Lets say I have a table with columns A , B , C , D and I want to do a match on column A and C

I see that you can either match a single column or all column in sqlite ie

- match column A
select * from table where A match 'cat' 

- match all columns
select * from table where table match 'cat'

Is it possible to match only columns A and C? If not, how do you guys get around this?

Thanks for your suggestion!

5条回答
对你真心纯属浪费
2楼-- · 2019-01-24 03:08

For the multiple column condition using MATCH use UNION for the 'OR' and use INTERSECT for the 'AND'

SELECT * FROM TBL_VIRTUAL_APPOINTMENT WHERE PATIENT_PREFIX MATCH 'D*' 
UNION 
SELECT * FROM TBL_VIRTUAL_APPOINTMENT WHERE patient_first_name MATCH 'K*'
查看更多
在下西门庆
3楼-- · 2019-01-24 03:09

Instead of specifying which columns to match to, you can specify the entire table to match to. For example:

-- Example schema
CREATE VIRTUAL TABLE mail USING fts3(subject, body);

-- Example table population
INSERT INTO mail(docid, subject, body) VALUES(1, 'software feedback', 'found it too slow');
INSERT INTO mail(docid, subject, body) VALUES(2, 'software feedback', 'no feedback');
INSERT INTO mail(docid, subject, body) VALUES(3, 'slow lunch order',  'was a software problem');

-- Example queries
SELECT * FROM mail WHERE subject MATCH 'software';    -- Selects rows 1 and 2
SELECT * FROM mail WHERE body    MATCH 'feedback';    -- Selects row 2
SELECT * FROM mail WHERE mail    MATCH 'software';    -- Selects rows 1, 2 and 3
SELECT * FROM mail WHERE mail    MATCH 'slow';        -- Selects rows 1 and 3
查看更多
何必那么认真
4楼-- · 2019-01-24 03:26

You can concatenate columns using the || operator:

SELECT * FROM table WHERE a || b MATCH 'cat'

I'm not sure about the MATCH function in SQLite though, I think it will usually throw an exception (see http://www.sqlite.org/lang_expr.html#match).

查看更多
家丑人穷心不美
5楼-- · 2019-01-24 03:28

I don't think you can use multiple MATCH operators in a single FTS query. Use column_name:target_term to search multiple columns using one full text search.

SELECT * FROM table WHERE table MATCH 'A:cat OR C:cat'

http://www.sqlite.org/fts3.html#termprefix

查看更多
做个烂人
6楼-- · 2019-01-24 03:30

Using sub-queries as workaround:

OR equivalent query, like a MATCH 'cat' OR c MATCH 'cat':

SELECT * FROM table
WHERE ROWID IN (
    SELECT ROWID FROM table WHERE a MATCH 'cat'
UNION
    SELECT ROWID FROM table WHERE b MATCH 'cat'
);

AND equivalent query, like a MATCH 'cat' AND c MATCH 'cat':

SELECT * FROM table WHERE ROWID IN (
    SELECT ROWID FROM table WHERE a MATCH 'cat'
    AND ROWID IN (
        SELECT ROWID FROM table WHERE b MATCH 'cat'
    )
);
查看更多
登录 后发表回答