Abedeh Golidoux
Abedeh Golidoux

Reputation: 1

Search for specific keyword in MYSQL

I'm almost new to mysql.

I wanted to write a query to search for specific keywords in a column where keywords are separated by the comma. but as I use the following code, it only returns the rows where I only have that specific keyword, not in combination with any other keywords.

In Table q16, I'm looking for a way to select rows that have my keyword in the "Area_of_concern" column, no matter if it's combined with other keywords or not:

 SELECT * 
 FROM `q16` 
 WHERE area_of_concern like '%more education is needed%'

Here's an input example:

q16_id area of concern

1   more education is needed
2   more enforcement, change in strategy
3   change in strategy
4   more education is needed, change in strategy
5   transportation issue, more enforcement, more education is needed

Where I'm looking to get the rows with the keyword "more education is needed". So I should see row 1, 4,5 in the output

Upvotes: 0

Views: 77

Answers (1)

gl00ten
gl00ten

Reputation: 1121

I think you should create a table where you have one column for keywords and one column for where those keywords are used: a foreign key for the q16 table in your case.

It will work much faster that way.

As for your question it is a duplicate of this one here, I believe.

How to search for rows containing a substring?

A quick try: try using double quotes instead of single ones, as in some systems, single quotes don't allow for escapes (special characters) inside them.

Upvotes: 0

Related Questions