【问题标题】:Symfony - addHaving - concat + regexpSymfony - addHaving - concat + regexp
【发布时间】:2019-01-29 19:34:43
【问题描述】:

原始查询:

select firstfield, secondfield, phone_number, thirdfield 
from table 
having CONCAT(firstfield, ' ', secondfield, ' ', thirdfield, ' ', fourthfield) regexp 'value' 
   and CONCAT(firstfield, ' ', secondfield, ' ', thirdfield, ' ', fourthfield) regexp 'value2' 
   and CONCAT(firstfield, ' ', secondfield, ' ', thirdfield, ' ', fourthfield) regexp 'value3'
   and CONCAT(firstfield, ' ', secondfield, ' ', thirdfield, ' ', fourthfield) regexp 'value4'

查询生成器

    $qb->select(
        'firstfield',
    'secondfield',
    'thirdfield',
    'fourthfield',
    )->from(Table, 'u');


$queryHaving = "CONCAT(firstfield, ' ', secondfield, ' ', thirdfield, ' ', fourthfield) regexp 'value'";
$qb->andhaving($queryHaving);

$queryHaving = "CONCAT(firstfield, ' ', secondfield, ' ', thirdfield, ' ', fourthfield) regexp 'value2'";
$qb->andhaving($queryHaving);

问题:

如何用正则表达式而不是函数来收集 concat?尝试使用 literal() 函数,但无法创建由于无法分配到的错误引发。

【问题讨论】:

  • 您将在哪个数据库上运行查询?
  • Oracle MySQL 学说

标签: regex doctrine-orm concat query-builder doctrine-dbal


【解决方案1】:

该查询似乎对我适用于以下两种形式的 MySQL:

select *
from test
having concat(field1, field2) regexp '^[FB].*' and
       concat(field1, field2) regexp 'o$';

select *
from test
where concat(field1, field2) regexp '^[FB].*' and
      concat(field1, field2) regexp 'o$';

查看演示here

我只是在想问题可能出在 CHAR 列上

因此,例如,一列将在CHAR(5) 上显示FOO<space><space>,而不是在VARCHAR(5) 上显示FOO。因此,在连接时,您会得到类似于 FOO<space><space>BAR<space><space> 的内容,因此正则表达式会失败。

但是,对于 SQLFiddle,情况似乎并非如此。它似乎没有添加空格。请参阅 here

无论如何,在您的应用上尝试一下可能是值得的:您使用的是 chars 还是 varchars?您可以尝试在列中添加trims,如下所示:

select *,concat(trim(field1), trim(field2))
from test
having concat(trim(field1), trim(field2)) regexp '^[FB].*' and
       concat(trim(field1), trim(field2)) regexp 'o$';


select *,concat(trim(field1), trim(field2))
from test
where concat(trim(field1), trim(field2)) regexp '^[FB].*' and
      concat(trim(field1), trim(field2)) regexp 'o$';

演示here

【讨论】:

  • 不,Symfony 上有一个命令->concat。如何在querybuilder中使用?
  • 也许你可以使用这样的东西:$queryHaving = "CONCAT(trim(firstfield), ' ', trim(secondfield), ' ', trim(thirdfield), ' ', trim(fourthfield)) regexp 'value'";
  • 是的,但是你应该把literal() var放在querybuilder中。在这些带有字面意义的场景中,我们有“concat(...) regexp ”,并且将其放入查询构建器会引发错误,例如您不能将这些与 regexp 之外的内容结合起来。我会将您标记为已解决,因为没有人回答或找不到使用文字 + 查询图片 + 拥有
猜你喜欢
  • 2016-11-22
  • 2017-01-15
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多