0

I have a sqlite table with following columns

SELECT * FROM table_one
+------+----------+--------------------------------------------------------+
|  ID  |   name   |                         dates                          |
+------+----------+--------------------------------------------------------+
| 1001 | SINGLE   | [{"dayOfMonth":5,"month":9}]                           | 
| 1002 | MULTIPLE | [{"dayOfMonth":2,"month":9},{"dayOfMonth":3,"month":9} |
+------+----------+--------------------------------------------------------+

Now if I query either one of below return correct info:

SELECT * FROM table_one WHERE dates LIKE '%dayOfMonth":3%' 
SELECT * FROM table_one WHERE dates LIKE '%dayOfMonth":5%' 

But I want to combine this two statement in one single line. I've tried this answer but didn't worked for me.

Here dates is a variable length and not exact. The IN operation is also not working. I want to perform this operation List<Item> getAllOf(List<String> queryStrings) which will return me a list of all matched item. What is the easiest way?

2 Answers 2

1

But I want to combine this two statement in one single line.

Why you just don't use OR operator between LIKE conditions ? :)

I mean

SELECT * FROM table_one WHERE dates LIKE '%dayOfMonth":3%' OR dates LIKE '%dayOfMonth":5%' 

General rule here: don't use REGEXP in WHERE conditions if you don't have to. With regexp you lose INDEX search advantage..

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

2 Comments

Yes I can use this method but my query string list may contain more than 100 items. Is there any other efficient way?
talking about performance regexp is not better then OR operator. Even OR LIKE is much faster if you have INDEX on dates column and if you do prefix search LIKE 'dayOfMonth":3%'. If you use regexp then INDEX is not used.
1

If you are going to do this with LIKE, you need to repeat the conditions, using OR:

SELECT * 
FROM table_one 
WHERE dates LIKE '%dayOfMonth":3%' OR dates LIKE '%dayOfMonth":5%' 

A regex solution would look like:

SELECT * FROM table_one WHERE dates REGEXP 'dayOfMonth":[35]' 

[35] is a character class, meaning that any of the two characters must be found.

A more generic approach if you have more than one character is to use | (which basically stands for "or" in regexes):

SELECT * FROM table_one WHERE dates REGEXP 'dayOfMonth":(3)|(5)' 

You can put as many characters as you want within the parentheses

1 Comment

REGEXP shows error. There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such function: REGEXP)

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.