Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query String contains

I've been trying to figure out how I can make a query with MySQL that checks if the value (string $haystack ) in a certain column contains certain data (string $needle), like this:

SELECT * FROM `table` WHERE `column`.contains('{$needle}') 

In PHP, the function is called substr($haystack, $needle), so maybe:

WHERE substr(`column`, '{$needle}')=1 
like image 695
arik Avatar asked Apr 08 '10 17:04

arik


People also ask

How do you check if a field contains a string in 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 I select a string in MySQL?

To select the row value containing string in MySQL, use the following syntax. SELECT *FROM yourTableName where yourColumnName like '%yourPattern%'; To understand the above syntax, let us first create a table.

How do you check if a string contains a word in SQL?

To check if string contains specific word in SQL Server we can use CHARINDEX function. This function is used to search for specific word or substring in overall string and returns its starting position of match. In case if no word found then it will return 0 (zero).

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

Quite simple actually:

SELECT * FROM `table` WHERE `column` LIKE '%{$needle}%' 

The % is a wildcard for any characters set (none, one or many). Do note that this can get slow on very large datasets so if your database grows you'll need to use fulltext indices.

like image 180
Wolph Avatar answered Sep 24 '22 17:09

Wolph