简体   繁体   中英

How to search with keywords? (PHP)

I was wondering how to generate a search SQL query for keywords:

I know:

SELECT * FROM table WHERE keyword LIKE %keyword%;

But how to work with multiple keywords.

I hope that makes sense...

SELECT * 
FROM `table`
WHERE keyword LIKE '%1%'
OR keyword LIKE '%2%'
OR keyword LIKE '%3%'

If I understand you right, this is what you're looking for

Please check with below codes:

You can use FULL TEXT SEARCH

SELECT col1 FROM table_name
WHERE MATCH (col1, col2, col3)
AGAINST ('keywords');

For this, you need to apply fulltext index on columns in which you want to search.

You can use IN operator too like -

SELECT col1 FROM table_name
WHERE col1 IN (keyword1, keyword2, keyword3, ...);

You can use a combination of percent signs and underscore wildcards

SELECT * FROM table WHERE keyword LIKE "%keywordA_keywordB%";

Or use CONTAINS and do something like:

SELECT * FROM table 
    WHERE keyword CONTAINS "%keywordA%" AND keyword CONTAINS "%keywordB%";

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