从json格式数据中选择查询

时间:2014-09-27 09:31:04

标签: php mysql sql drupal

我只想要一个选择查询,它可以选择1986年以后出生的所有用户uid。

我尝试过此查询,但只选择匹配的号码LIKE搜索。

SELECT uid FROM {profile_values} WHERE  value LIKE  '%1986%'

这是SQL查询

CREATE TABLE IF NOT EXISTS `profile_values` (
  `fid` int(10) unsigned NOT NULL DEFAULT '0',
  `uid` int(10) unsigned NOT NULL DEFAULT '0',
  `value` text,
  PRIMARY KEY (`uid`,`fid`),
  KEY `fid` (`fid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `profile_values` (`fid`, `uid`, `value`) VALUES
(5, 1, 'a:3:{s:3:"day";s:2:"27";s:5:"month";s:1:"6";s:4:"year";s:4:"1986";}'),
(5, 3, 'a:3:{s:3:"day";s:2:"15";s:5:"month";s:1:"9";s:4:"year";s:4:"1910";}'),
(5, 4, 'a:3:{s:3:"day";s:2:"26";s:5:"month";s:1:"6";s:4:"year";s:4:"1986";}'),
(5, 5, 'a:3:{s:5:"month";s:1:"4";s:3:"day";s:2:"26";s:4:"year";s:4:"2014";}'),
(5, 6, 'a:3:{s:3:"day";s:2:"26";s:5:"month";s:1:"4";s:4:"year";s:4:"2014";}'),
(5, 7, 'a:3:{s:5:"month";s:1:"4";s:3:"day";s:2:"26";s:4:"year";s:4:"2014";}'),
(5, 8, 'a:3:{s:3:"day";s:2:"26";s:5:"month";s:1:"4";s:4:"year";s:4:"1987";}'),
(5, 17, 'N;'),
(5, 18, 'N;'),

1 个答案:

答案 0 :(得分:0)

您可以尝试使用REGEXP。 REGEXP匹配条件类似于LIKE,但LIKE仅使用通配符执行简单模式匹配"%"和" _",REGEXP执行复杂的正则表达式模式匹配,允许它匹配比LIKE更大范围的字符串模式。

SELECT uid FROM {profile_values} 
WHERE value REGEXP '[1][9][8][6-9]|[1][9][9][0-9]|[2-9][0-9][0-9][0-9]';

在这里,我们确保我们的表达式只考虑4位数字,而且数字应该大于1986

so [1][9][8][6-9] -->> makes sure number is between 1986 to 1989
next [1][9][9][0-9] -->> makes sure number is between 1990 to 1999
last [2-9][0-9][0-9][0-9] -->> makes sure number is between 2000 to 9999

请参阅http://www.tutorialspoint.com/mysql/mysql-regexps.htm了解更多