简体   繁体   中英

Mysql - How to get wildcard search on words of sting

I have a words for search is 'samsung s3' while I have products name in database is 'samsung galaxy s3'.

and my query is :

select * from products where status=1 and name like '%samsung s3%'

so I am getting no result , is it possible we can serach word within name string.

You have to use keywords. Probably when you save the product name to be able search it with all keywords, add separate keywords for everything that you want to be searched with. Now that is the best way for me.

Eg: so for Samsung Galaxy s3 I would save 3 entries in my keywords table, ie : Samsung, Galaxy, s3

Else you can split your search string and search with each word in the DB.

Eg:

select * from products where status=1 and (name like '%samsung%' or name like '%s3%')

There is a full text search in MS SQL. It might be available in MySQL or its equivalent.

Edit : searched and confirmed full text search exist for MySQL. Here is a link But be warned Full text search is slow. Specially if its a large database.

MySql has a neat natural language search features that works on text and varchar columns.

https://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

The idea is you would do:

select * from products where status=1 MATCH (name) AGAINST ('samsung s3'); 
  1. Create full text index on name field

     ALTER TABLE products ADD FULLTEXT (name); 
  2. Run query :-

     SELECT * FROM products WHERE status=1 AND name MATCH(name) AGAINST ('+samsung +s3' IN BOOLEAN MODE); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM