0

I'm trying to check if my text contain any tag from my SQL table. I created a function which changing my text to lowercase and remove everything what is not in [A-Za-z-0-9] for -, example:

Hello! I want to rent my flat which is in the best district - Ealing.

and after using my PHP function:

hello-i-want-to-rent-my-flat-which-is-in-the-best-district-ealing

I have a SQL table:

| ID | PHRASE |              TAGS                                   |
| 1  | London | kings-cross,heathrow,camden-town,ealing             |
| 2  | Berlin | charlottenburg-wilmersdorf,friedrichshain-kreuzberg |

And now - how I can check if my formated text (by my PHP function) contain PHRASE or any tag from TAGS column? I need a query, something like that:

SELECT * FROM `table` WHERE 'hello-i-want-to-rent-my-flat-which-is-in-the-best-district-ealing' LIKE CONCAT('%-',phrase,'-%') OR 'hello-i-want-to-rent-my-flat-which-is-in-the-best-district-ealing' LIKE CONCAT('%-',tags,'-%')

But I have more than one tag in TAGS column, so now I have to make a few SQL requests (if I have 3 tags, I need to explode this tags and make 3 SQL requests to check if text contain any tag). Is it possible to check it in one query?

Thanks.

Pavel K
  • 235
  • 3
  • 15
  • 2
    Your table design is the problem. Never store multiple values in a single column! – juergen d May 19 '18 at 04:53
  • Can you please add some sample data and expected output. –  May 19 '18 at 05:00
  • @KKK I cant because my example query return nothing... In my text: `hello-i-want-to-rent-my-flat-which-is-in-the-best-district-ealing` is word: **ealing** and in my SQL table column TAGS is: kings-cross,heathrow,camden-town,**ealing** so I need a query which will return record ID = 1. – Pavel K May 19 '18 at 05:07
  • 2
    See https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574#3653574 – Bill Karwin May 19 '18 at 05:07

0 Answers0