0

is it possible to use variables in LIKE pattern matches in sqlite queries? i have the tables:

Table 1:

id  names  linker  
1   williams, john; jones, lisa   A1
2   jones, lisa                   A1
3   sanders, karim                A2
...

Table 2:

index  name      linker
X      williams  A1
Y      jones     A1
Z      sanders   A2

i want to join Table 1 to Table 2 on the linker column, grouped by Table2.index, but only consider entries from Table 1 where Table1.name is a substring of Table2.names, ie Table2.names LIKE '%Table1.name%'. in above tables williams entry with id 1 in table1 would be associated with index X in table2, but entry with id 2 of table1 would not. how can a column be used inside a pattern match? this does not work

select * from table1 join table2 on (table1.linker == table2.linker) where names like '%' + name + '%' group by table1.index

1 Answer 1

2

In SQL, + is used to add numbers. To concatenate strings, use ||:

SELECT *
FROM table1
JOIN table2 USING (linker)
WHERE names LIKE '%' || name || '%'
GROUP BY table1.id;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.