Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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:

mysql_query("

SELECT *

FROM `table`

WHERE `column`.contains('{$needle}')

");

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

WHERE substr(`column`, '{$needle}')=1

1 Answer

0 votes
by (40.7k points)
edited by

You can use this query:

Query:

mysql_query("SELECT *

FROM `table`

WHERE `column` LIKE '%{$needle}%' ");

Want to learn SQL from scratch? Here's is the right video for you on SQL provided by Intellipaat

Here, ‘ %’ is a wildcard that is used for any character.

Note: For very large datasets this can get slow. So, if your database grows then you need to use fulltext indices.

Related questions

0 votes
1 answer
asked Jul 18, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Dec 31, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Oct 7, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Jul 29, 2019 in SQL by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...