原始查询:
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'
QueryBuilder的
$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);
问题:
如何用regexp收集concat不作为函数?尝试使用literal()函数,但不可能创建不可能分配到的错误抛出。
对于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列
因此,例如,一列将在FOO<space><space>
上使用CHAR(5)
而不是在FOO
使用VARCHAR(5)
。因此,当连接时,你会有类似于FOO<space><space>BAR<space><space>
的东西,因此正则表达式会失败。
但是,使用SQLFiddle似乎并非如此。它似乎没有添加空格。见here。
无论如何,它可能值得尝试你的应用程序:你使用字符或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$';
但是zzxswい