SQLite Forum

Help with a search command
Login
Assuming performance is never going to be an issue, then something like this might appeal to you:


```sql
.mode table

CREATE TABLE sqlDiaryData(
  fldKey0 TEXT,
  fldKey1 TEXT,
  fldKey2 TEXT,
  all_fields AS (fldKey0 || fldKey1 || fldKey2)
);
INSERT INTO sqlDiaryData VALUES ('this', 'is', 'test'), ('abc', 'def', 'test'), ('123abc345', 'test', 'foo'), ('123', 'xyz', 'bar');

-- 2 rows expected
WITH search(search_term) AS(VALUES ('test'), ('abc'))
SELECT dd.*
  FROM sqlDiaryData dd
  JOIN search       s  ON dd.all_fields LIKE '%' || s.search_term || '%'
 GROUP BY dd.rowid
HAVING Count(*) = (SELECT Count(*) FROM search);
```

```bash
+-----------+---------+---------+------------------+
|  fldKey0  | fldKey1 | fldKey2 |    all_fields    |
+-----------+---------+---------+------------------+
| abc       | def     | test    | abcdeftest       |
| 123abc345 | test    | foo     | 123abc345testfoo |
+-----------+---------+---------+------------------+
```