Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在MYSQL中使用REGEXP来匹配数组中的日期?_Php_Mysql_Regex_Laravel - Fatal编程技术网

Php 如何在MYSQL中使用REGEXP来匹配数组中的日期?

Php 如何在MYSQL中使用REGEXP来匹配数组中的日期?,php,mysql,regex,laravel,Php,Mysql,Regex,Laravel,我试图在很多年中匹配表的发布日期起的年份。我使用的是laravel(PHP) 我写过这样的东西: if($this->request->has('publication_years')){ $years = implode('-[0-9]{2}-[0-9]{2}|', explode(',', $this->request->get('publication_years'))) . '-[0-9]{2}-[0-9]{2}'; $model = $mode

我试图在很多年中匹配表的发布日期起的年份。我使用的是laravel(PHP)

我写过这样的东西:

if($this->request->has('publication_years')){
    $years = implode('-[0-9]{2}-[0-9]{2}|', explode(',', $this->request->get('publication_years'))) . '-[0-9]{2}-[0-9]{2}';

    $model = $model->whereExists(function ($query) use ($years)
    {
        $query->select(DB::raw(1))
            ->from('patent')
            ->whereRaw('patent.id = document.documentable_id')
            ->whereRaw('document.documentable_type = "patent"')
            ->whereRaw('(patent.publication_date REGEXP (' . $years . '))');
    });
}
这些年来就像出版年=20152016。我使用
分隔符分解它们,然后对它们进行内爆,以使REGEXP模式与日期匹配。这之后几年的垃圾堆就像是
2015-[0-9]{2}-[0-9]{2}2016-[0-9]{2}-[0-9]{2}

这在上个月起作用,但我仍然会出错,所以我猜MYSQL还有其他一些语法。我看到的错误是:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[0-9]{2}-[0-9]{2}|2016-[0-9]{2}-[0-9]{2})))' at line 1 (SQL: select count(*) as aggregate from `document` where exists (select * from `folder` where `document`.`folder_id` = `folder`.`id` and (exists (select * from `user` inner join `accessible_by_user` on `user`.`id` = `accessible_by_user`.`user_id` where `accessible_by_user`.`accessible_id` = `folder`.`id` and `accessible_by_user`.`accessible_type` = folder and `user_id` = 1) or exists (select * from `role` inner join `accessible_by_role` on `role`.`id` = `accessible_by_role`.`role_id` where `accessible_by_role`.`accessible_id` = `folder`.`id` and `accessible_by_role`.`accessible_type` = folder and `role_id` in (1)))) and exists (select 1 from `patent` where patent.id = document.documentable_id and document.documentable_type = "patent" and (patent.publication_date REGEXP (2015-[0-9]{2}-[0-9]{2}|2016-[0-9]{2}-[0-9]{2}))))

我做错了什么?如何更正此问题?

以下是准备在MySQL中使用正则表达式的方法:

$this_request_get_publication_years = "2015,2016,2017";
$years = '^(' . implode('|', explode(',',      
$this_request_get_publication_years)) . ')-[0-9]{2}-[0-9]{2}';
echo "(patent.publication_date REGEXP '" . $years . "')";
// => (patent.publication_date REGEXP '^(2015|2016|2017)-[0-9]{2}-[0-9]{2}')
REGEXP'^(2015 | 2016 | 2017)-[0-9]{2}-[0-9]{2}'
将只获取以下记录:

  • ^
    -在字符串开头,让
  • (2015年| 2016年| 2017年)
    -三年中的任何一年(当然支持更多年)
  • -[0-9]{2}
    -后跟两位数字的连字符
  • -[0-9]{2}
    -同上

后两个子模式可以收缩为
([0-9]{2}{2}

用单引号将正则表达式括起来,如
REGEXP'(2015-[0-9]{2}..)
REGEXP'^(2016 | 2015)'Wow。就这样。我现在觉得自己很愚蠢。不管怎样,它成功了。谢谢你们。我认为我的正则表达式是错误的。所以,使用
$years=“^(.str_replace(',','|',$this->request->get('publication_years'))。-[0-9]{2}-[0-9]{2}和更改
->whereRaw('(patent.publication_date REGEXP('.$years'))
->whereRaw(“(patent.publication_date REGEXP\”.$years.”)。对吗?