Select 条不以 "xxx" 开头的记录加入了另一条 table

Select records that do not start with "xxx" joined onto another table

我正在尝试获取项目列表(最终将被删除)。我内心 select returns 以下项目来自 collections table (这是正确的)。

我想使用上面的结果加入 files table 和 select 不是以上面路径开头的项目,这样我就可以删除它们。

我尝试使用此查询 select 他们但是,如果我过滤结果,结果集中存在我想排除的记录。

SELECT files.path, c.path FROM files LEFT JOIN (
  SELECT json_extract(info, '$.path') AS path FROM collections 
  WHERE 
  json_extract(info, '$.path') like 'C:/Users/untun/Documents%'
  AND json_extract(info, '$.path') != 'C:/Users/untun/Documents'
) AS c ON c.path LIKE files.path || '%'

-- Adding the next lines returns the two records below
-- GROUP BY files.path
-- HAVING files.path like 'C:/Users/untun/Documents/vscode/projects/csharp%'

您必须反转运算符 LIKE 的操作数并过滤掉 WHERE 子句中不匹配的行:

SELECT f.path 
FROM files AS f 
LEFT JOIN (
  SELECT json_extract(info, '$.path') AS path 
  FROM collections 
  WHERE json_extract(info, '$.path') LIKE 'C:/Users/untun/Documents_%'
) AS c ON f.path LIKE c.path || '%'
WHERE c.path IS NULL;

另外,条件:

json_extract(info, '$.path') like 'C:/Users/untun/Documents%'
AND 
json_extract(info, '$.path') != 'C:/Users/untun/Documents'

可以简化为:

json_extract(info, '$.path') LIKE 'C:/Users/untun/Documents_%'