Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string contains any string of a column in MySQL and vice versa?

By vice versa, I mean, check if any of the strings in a column contains the string.

Example:

String a = "Peperoni"

MySQL column:

{
    "PPeperoni_123",
    "roni",
    "hello world"
}

It should return the first 2 rows.

(Note: im looking for the query string for this)

like image 365
Zananok Avatar asked Apr 07 '13 12:04

Zananok


People also ask

How do you check if a string contains a substring MySQL?

MySQL query string contains using INSTRINSTR(str, substr) function returns the index of the first occurrence of the substring passed in as parameters. Here str is the string passed in as the first argument, and substr is the substring passed in as the second argument.

How do you check if a column contains a value in MySQL?

$query = "SELECT * FROM my_table WHERE categories LIKE '2'"; $rows = mysql_query($query); This returns row if column only has value 2 but not 1,2,3 or 2,12.

How do I find a string in MySQL?

MySQL LOCATE() Function The LOCATE() function returns the position of the first occurrence of a substring in a string. If the substring is not found within the original string, this function returns 0. This function performs a case-insensitive search. Note: This function is equal to the POSITION() function.

Which operator in MySQL is used to check string contains specific characters?

Check if String Contains Certain Data in MySQL Using the LIKE Operator in MySQL. Another alternative to find the existence of a string in your data is to use LIKE . This operator is used along with the WHERE clause to look for a particular string.


1 Answers

Not sure if I understand you correctly, but are you maybe looking for this:

SELECT ... WHERE column LIKE "%string%" OR string LIKE CONCAT("%", column, "%")
like image 194
mrks Avatar answered Sep 22 '22 19:09

mrks