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

罗汉

我试图匹配很多年以来从表的publication_date开始的年份。我正在使用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 . '))');
    });
}

岁月如来?publication_years=2015,2016我使用,定界符将它们爆炸,然后将其内爆以使其与日期匹配的方式制作REGEXP模式。此后多年的倾倒就像2015-[0-9]{2}-[0-9]{2}|2016-[0-9]{2}-[0-9]{2}

这在http://regexr.com/上有效,但是我仍然遇到错误,因此我猜还有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) -3年之一(肯定会得到更多支持)
  • -[0-9]{2} -连字符后跟2位数字
  • -[0-9]{2} -同上

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

查找并匹配数组中的日期

来自分类Dev

如何在django1.4中使用mysql regexp

来自分类Dev

如何在MySQL查询中使用Regexp?

来自分类Dev

如何匹配数组中的特定值

来自分类Dev

如何使用JSON匹配数组的值

来自分类Dev

如何在SAS中匹配数据

来自分类Dev

如何匹配数组并显示不在数组中的数组?

来自分类Dev

如何在弹性搜索中匹配数组中的所有参数传递?

来自分类Dev

如何在SQL Server中使用行号获取匹配数据?

来自分类Dev

如何匹配数组中的元素并仅打印匹配项?

来自分类Dev

如何在 MySQL 中返回 REGEXP 匹配项

来自分类Dev

如何在MySQL中按关联表中的匹配数对行进行排序?

来自分类Dev

如何在Javascript中用逗号分隔值匹配数组?

来自分类Dev

匹配数组中的整数

来自分类Dev

在 Scala 中匹配数组

来自分类Dev

如何在Fortran子例程中使用可分配数组?

来自分类Dev

如何在PHP中使用爆炸值分配数组变量?

来自分类Dev

如何在php表单提交中使用isset输入分配数组多个值

来自分类Dev

如何在 C 中使用 malloc 和 realloc 在结构中正确分配数组?

来自分类Dev

匹配数组的值,而不管javascript中的位置如何

来自分类Dev

VBA:如何匹配数组中的多个条件?

来自分类Dev

使用ActiveRecord在PostgreSQL中匹配数组值

来自分类Dev

MongoDB-如何在$ group中使用$ in时返回数组中的匹配项?

来自分类Dev

如何在 Visual Basic 中使用 RegExp 中的 vbNewLine

来自分类Dev

如何在增量搜索中显示匹配数?

来自分类Dev

Perl:如何在整个文本中匹配数据

来自分类Dev

我如何比较 mysql 多个表中的匹配数据?

来自分类Dev

如何过滤数组以匹配数组

Related 相关文章

热门标签

归档