Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you match multiple column in a table with SQLite FTS3?

Tags:

sql

sqlite

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!

like image 424
Unikorn Avatar asked Oct 30 '10 04:10

Unikorn


People also ask

How do I SELECT multiple columns in SQLite?

To select multiple columns from a table, simply separate the column names with commas!

How do I update multiple columns in SQLite?

How do I UPDATE multiple columns in SQLite? First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause.

How do I add multiple columns in SQLite?

How do I add multiple columns in SQLite? SQLite does not support adding multiple columns to a table using a single statement. To add multiple columns to a table, you must execute multiple ALTER TABLE ADD COLUMN statements.

How can I get column names from a table in SQLite?

Connect to a database using the connect() method. Create a cursor object and use that cursor object created to execute queries in order to create a table and insert values into it. Use the description keyword of the cursor object to get the column names.


4 Answers

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
like image 30
Pete Avatar answered Nov 08 '22 02:11

Pete


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

like image 78
Rob Seed Avatar answered Nov 08 '22 03:11

Rob Seed


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*'
like image 5
Krunal Shah Avatar answered Nov 08 '22 03:11

Krunal Shah


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'
    )
);
like image 2
LS_ᴅᴇᴠ Avatar answered Nov 08 '22 03:11

LS_ᴅᴇᴠ