Kemal Güler
Kemal Güler

Reputation: 614

How to SELECT with all index of SUBSTRING_INDEX

I have a table like this

table foo1

+----+------------------------------+
| Id | Content                      |
+----+------------------------------+
| 1  | Hello World                  |
+----+------------------------------+
| 2  | Hello Users                  |
+----+------------------------------+
| 3  | This post submitted by users |
+----+------------------------------+
| 4  | C# Programming               |
+----+------------------------------+

I send a parameter to stored procedure like 'Hello,users,post'. I want to split parameter with comma(,) and get all rows which contains this indexes (Hello or users or post). If i send 'Hello,users' return table should be like this (contains hello or users)

+----+------------------------------+
| 1  | Hello World                  |
+----+------------------------------+
| 2  | Hello Users                  |
+----+------------------------------+
| 3  | This post submitted by users |
+----+------------------------------+

I have tried this query

SELECT
    *
FROM foo1
WHERE foo1.Content 
LIKE CONCAT('%',SUBSTRING_INDEX('Hello, Users, Post', ',', 1),'%');

but it returns rows which contain just 'Hello'. Parameter's word is flexible. It can contains countless word. It can be 'Hello, post, users' or 'Hello' or 'Hello,post'. How can I solve this?

Thanks.

Upvotes: 1

Views: 210

Answers (1)

Matt
Matt

Reputation: 15061

Use the REGEXP function.

SELECT * 
FROm foo1 
WHERE foo1.Content REGEXP 'Hello|Users|Post'; 

Upvotes: 4

Related Questions