简体   繁体   中英

Search in mysql column against provided json value

I have a table contents. Columns -> id, title, description.

 CREATE TABLE IF NOT EXISTS `contents` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `description` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- Dumping data for table contents --

INSERT INTO `contents` (`id`, `title`, `description`) VALUES
(1, 'My fab phone', 'I own an iphone 5c.'),
(2, 'I hate my phone', 'I have a Samsung S4. :(');

I have a json vaue made by input from user. Its like ->

[{"devices":"iPhone 5c"},{"devices":"Samsung S4"}] 

I would like to search against it against the title, description comlumn in mysql.. my application is in php. is it even possible in mysql ? or I have to manipulate the json run it in loop and then simple search in mysql ? ...If its possible in mysql then please help

Just convert table in MyISAM storage engine

SELECT  *
FROM    pages
WHERE   MATCH(title, description) AGAINST ('keyword' IN BOOLEAN MODE)

This will be much faster if you create a FULLTEXT index on your columns:

CREATE FULLTEXT INDEX fx_pages_title_description ON pages (title, description)

, but will work even without the index.

像这样使用common_schema

select common_schema.extract_json_value(t.title,'/title') , common_schema.extract_json_value(t.description,'/description') from contents t ;
SELECT *
FROM `contents`
WHERE
    (`title` = 'iPhone 5c' AND `description` = 'iPhone 5c')
    OR
    (`title` = 'iPhone 5c' OR `description` = 'iPhone 5c');

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