简体   繁体   中英

Searching for words, not a phrase in mysql < 5.6.4

I'm looking for a way to search words in mysql table. Considering I have a table like this:

CREATE TABLE IF NOT EXISTS `q_news` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(250) NOT NULL,
  `short_content` text NOT NULL,
  `content` text NOT NULL,
  `description` varchar(400) NOT NULL,
  `keywords` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;

And in one of the records, in title field I have "The quick brown fox jumps over the lazy dog"

Now, I want to get this record, when looking for "quick lazy" - I can't use match against, because it's innoDb and mysql version is below 5.6.4. What's the solution then? The only thing which comes to my mind is :

select * from q_news where `title` like '%quick%%lazy%'

But somehow I don't like this solution, is there a better way?

ps. I didn't know how to phrase my question, so couldn't google it, sorry if this is duplicate.

How about this?

SELECT * FROM q_news WHERE `title` LIKE '%quick%' AND `title` LIKE '%lazy%'

Using 2 LIKE can solve your problem.

尝试这个:

SELECT * FROM q_news WHERE MATCH(title) AGAINST ('quick lazy');

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