0

Yes, this is a question that has been beaten to death, yet I believe its a tad different.

Please consider this MySQL table test:

enter image description here

The desired resultset is:

enter image description here

In words, how can the first 3 records (ids 1, 2, 5) be selected using exactly this string - 'Cinema, Entertainment' and in one single query? That is, how can A be compared with the string above and have it return the record if even one 'word' matches?

Have tried the following:

  • SELECT * FROM test WHERE A LIKE "%Cinema, Entertainment%"
  • SELECT * FROM test WHERE INSTR(A, 'Cinema, Entertainment') > 0

Both return just the first record, doing an exact match.

SELECT * FROM test WHERE A LIKE '%Cinema%' OR A LIKE '%Entertainment%' does work, but I do not want to tokenize the available string. The string could also have multiple CSVs.

Very new to regular expressions, and cannot seem to form the right query.

Have seen these cool answers,:

...but still struggling.

Thanks in advance!

9
  • You realize you're using the wrong data structure, right? Commented Mar 21, 2017 at 4:55
  • I think you need to go for full text search Commented Mar 21, 2017 at 4:57
  • @shmosel, thank you! You mean there should be no CSV in a single column? Commented Mar 21, 2017 at 4:59
  • @VikasUmrao, thanks, but beats me. Database newbie that I am, not sure what full text search means... Commented Mar 21, 2017 at 5:00
  • Downvoters, please hint as to why your downvote. Commented Mar 21, 2017 at 5:02

2 Answers 2

1

The below query will help on this requirement. But you need to make sure the A column FULLTEXT Indexed.

ALTER TABLE `test` ADD FULLTEXT(`A`);

SELECT * FROM `test` WHERE (match(A) against('Cinema, Entertainment' IN BOOLEAN MODE));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Naga. SELECT * FROM test WHERE match(A) against('Cinema, Entertainment' IN BOOLEAN MODE) works.
0

Use mysql FIND_IN_SET function to get your desired result.

select * from `test` where find_in_set('Cinema',`A`) OR find_in_set('Entertainment',`A`);

Demo

1 Comment

Thanks! But like I said, I do not want to tokenize the available string "Cinema, Entertainment". However, a new learning this.

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.