SQLite FTS5表搜索不一致

hwamh0ep  于 2023-10-23  发布在  SQLite
关注(0)|答案(1)|浏览(142)

我有一个TRANSACTIONS表,它是用以下查询创建的:

CREATE TABLE TRANSACTIONS (ID INTEGER PRIMARY KEY, DESC VARCHAR(255), COMMENT VARCHAR(1024), INVOICE VARCHAR(255));

然后,我创建了FTS5虚拟表,查询如下:

CREATE VIRTUAL TABLE TRANSACTIONS_FTS USING FTS5(
    content="TRANSACTIONS",
    content_rowid="ID",
    DESC,
    COMMENT,
    INVOICE,
);

然后我使用以下查询填充了TRANSACTIONS_FTS:

INSERT INTO TRANSACTIONS_FTS (rowid, DESC, COMMENT, INVOICE) SELECT ID, DESC, COMMENT, INVOICE FROM TRANSACTIONS;

现在我正在运行两个搜索查询。第一个:

SELECT * FROM TRANSACTIONS_FTS WHERE TRANSACTIONS_FTS MATCH 'cabana';

一共有27个结果
而第二个

SELECT DISTINCT T.*
FROM TRANSACTIONS AS T
JOIN (
    SELECT *
    FROM TRANSACTIONS_FTS
    WHERE TRANSACTIONS_FTS MATCH 'cabana'
) AS T_MATCH ON T.ID = T_MATCH.rowid;

给出0个结果。为什么不给出相同的结果?如何调试Join?

oogrdqng

oogrdqng1#

我终于成功了。除非您直接指定,否则FTS表中的rowid似乎不会返回。因此,搜索查询的工作是:

SELECT T.*
        FROM TRANSACTIONS AS T
        JOIN (
            SELECT rowid
            FROM TRANSACTIONS_FTS
            WHERE TRANSACTIONS_FTS MATCH 'Cabana' -- Add more words as needed
        ) AS T_MATCH ON T.ID = T_MATCH.rowid

相关问题