Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.4k 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)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...