0

Assume I've the following table in sqlite database

Table name A with column A1 and A2

enter image description here

Table name B with column B1 and B2

enter image description here

I want to query like following

SELECT A1 FROM A, B WHERE A.A2 LIKE '%'+B.B2+'%' AND B.B1 = 1

I need the result of 2 rows which contains 1,4

But always it returns 0 rows.

2
  • Tip of today: Switch to modern, explicit JOIN syntax! Easier to write (without errors), easier to read and maintain, and easier to convert to outer join if needed! Commented Oct 20, 2020 at 11:49
  • / separated items is a mess. Commented Oct 20, 2020 at 11:51

2 Answers 2

2

The string concatenation operator in SQLite is ||. And you can express this as a JOIN:

SELECT A1
FROM A JOIN
     B 
     ON A.A2 LIKE '%' || B.B2 || '%' 
WHERE B.B1 = 1;

Note that || is the SQL standard string concatenation operator. + is overloaded in just a handful of databases owned by Microsoft (mostly).

Sign up to request clarification or add additional context in comments.

1 Comment

Wow it works great. I tried string concatination using +, concat keyword, [ ] square brackets. nothing worked. Finally || this works. Thanks @Gordon
0

Maybe for this sample data a condition like:

A.A2 LIKE '%' || B.B2 || '%'

will work, but in general the condition should include the separator '/' of the items.

For example if you have a row in A with A2 equal to '\aa\bb\' then the above condition will return incorrect results if you search for 'a', because '\aa\bb\' LIKE '%a%' is TRUE.
You can check here how you will get wrong results.

So you must use this query:

SELECT DISTINCT A1
FROM A INNER JOIN B 
ON A.A2 LIKE '%/' || B.B2 || '/%' 
WHERE B.B1 = 1;

See the demo.
Results:

> | A1 |
> | -: |
> |  1 |
> |  4 |

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.