Stan
Stan

Reputation: 26511

MySQL - Filter result

I want to filter result of my SQL query. I want to select everything that has some specific text in some column.

Example:

SELECT * FROM categories WHERE (name has 'abc' values in it's value ex. MyabcCategory)

Also maybe it is not very good idea to do that in query, maybe it is better to get all and then filter array instead? But I don't know how to do that aether.

Upvotes: 1

Views: 195

Answers (3)

Pascal MARTIN
Pascal MARTIN

Reputation: 401152

You want to use the LIKE operator, with % to match any character before and after your specific word :

select *
from categories
where name like '%abc%';


But note that doing so, MySQL will scan each line of the table, every time the query is executed... which might not be great if you have a lot of data.

If you're searching for some kind of text, you might either want to :

  • Use a FULLTEXT index, if you're working with MyISAM tables.
  • Or, use a solution that's separated from MySQL, with a specific indexing/search engine, such as Solr.

Upvotes: 3

Alessandro
Alessandro

Reputation: 1336

SELECT * FROM categories WHERE name LIKE '%abc%'

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382806

Use LIKE with % wildcard:

SELECT * FROM categories WHERE name LIKE '%abc%'

This will give you all the records that have abc somewhere in them.

You can learn more about it here :)

Upvotes: 5

Related Questions