简体   繁体   English

在mysql列中搜索提供的json值

[英]Search in mysql column against provided json value

I have a table contents. 我有一张桌子的内容。 Columns -> id, title, description. 列-> ID,标题,描述。

 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 -- -转储表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. 我有一个来自用户输入的json值。 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. 我想针对它的标题,描述comlumn在mysql中搜索它。我的应用程序在php中。 is it even possible in mysql ? 在mysql中甚至有可能吗? or I have to manipulate the json run it in loop and then simple search in mysql ? 还是我必须操纵json使其在循环中运行,然后在mysql中进行简单搜索? ...If its possible in mysql then please help ...如果可能在mysql中使用,请帮助

Just convert table in MyISAM storage engine 只需在MyISAM存储引擎中转换表

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: 如果在列上创建FULLTEXT索引,则速度会更快:

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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