Php 我可以在原则中的WHERE-in-ARRAY查询中实现类似条件吗?
我正在使用Doctrine QueryBuilder创建一个查询,通过电子邮件结尾选择一组用户。我有一系列电子邮件结尾,例如Php 我可以在原则中的WHERE-in-ARRAY查询中实现类似条件吗?,php,arrays,symfony,doctrine-orm,sql-like,Php,Arrays,Symfony,Doctrine Orm,Sql Like,我正在使用Doctrine QueryBuilder创建一个查询,通过电子邮件结尾选择一组用户。我有一系列电子邮件结尾,例如['@someservice.com','@anotherservice.com'] 我知道我可以通过中的WHERE in选择数组中的字符串: $qb= $this ->createQueryBuilder('u') ->orderBy('u.id', 'asc') ->where('u.email IN (:emails)')
['@someservice.com','@anotherservice.com']
我知道我可以通过中的WHERE in
选择数组中的字符串:
$qb= $this
->createQueryBuilder('u')
->orderBy('u.id', 'asc')
->where('u.email IN (:emails)')
->setParameter('emails', [
'@someservice.com',
'@anotherservice.com'
]);
然而,此查询用于查找字符串的确切匹配项,因此该查询当然将返回一个空的resultset
这就是为什么我想在阵列上进行类似的搜索,但要按照以下方式进行:
$qb= $this
->createQueryBuilder('u')
->orderBy('u.id', 'asc')
->where('u.email LIKE IN (:emails)')
->setParameter('emails', [
'%@someservice.com',
'%@anotherservice.com'
]);
不幸的是,它失败了。做这样的查询有什么语法上的甜点吗?或者我必须用一堆
或调用来进行查询吗?你不能那样做都是为了喜欢,喜欢不用跟在一起
您只能使用或:
->where("u.email LIKE '%@someservice.com' OR u.email LIKE '%@anotherservice.com'")
你不能那样做都是为了喜欢,喜欢不用跟在一起
您只能使用或:
->where("u.email LIKE '%@someservice.com' OR u.email LIKE '%@anotherservice.com'")
我也通过以下方式取得了类似的成就:
foreach($emailEndings as $index => $ending) {
$qb->orWhere("u.email LIKE :email$index");
$qb->setParameter("email$index", $ending);
}
索引很重要,否则只选择数组的最后一个条目作为索引
请注意,如果您有多个where子句,您将由于或where
子句而陷入麻烦。您不能将其更改为和where
,因为这将导致结果集为空。在这种情况下,您需要将和的条件分组到或中,如下所示:
/**
* @param QueryBuilder $qb
*/
protected function addInternalFilter(QueryBuilder $qb)
{
$conditions = [];
foreach ($this->emailEndings as $index => $ending) {
$conditions[] = "u.email LIKE :string$index";
$qb->setParameter("string$index", $ending);
}
if (empty($conditions)) {
throw new \LogicException('Conditions are empty.');
}
$qb->andWhere(new Orx($conditions));
}
我也通过以下方式取得了类似的成就:
foreach($emailEndings as $index => $ending) {
$qb->orWhere("u.email LIKE :email$index");
$qb->setParameter("email$index", $ending);
}
索引很重要,否则只选择数组的最后一个条目作为索引
请注意,如果您有多个where子句,您将由于或where
子句而陷入麻烦。您不能将其更改为和where
,因为这将导致结果集为空。在这种情况下,您需要将和的条件分组到或中,如下所示:
/**
* @param QueryBuilder $qb
*/
protected function addInternalFilter(QueryBuilder $qb)
{
$conditions = [];
foreach ($this->emailEndings as $index => $ending) {
$conditions[] = "u.email LIKE :string$index";
$qb->setParameter("string$index", $ending);
}
if (empty($conditions)) {
throw new \LogicException('Conditions are empty.');
}
$qb->andWhere(new Orx($conditions));
}
LIKE IN
不是公认的SQL语法AFAIK,因此可能不受支持。这并不妨碍您使用一个helper函数对其进行扩展,该函数将其转换为类似的内容<代码>拥有
始终是最后的选择,这非常适合于中的
类似于
中的
不是公认的SQL语法AFAIK,因此可能不受支持。这并不妨碍您使用一个helper函数对其进行扩展,该函数将其转换为类似的内容拥有
始终是最后的选择,这非常适合中的
。我希望避免在每次添加新电子邮件结尾时维护select查询。我想在配置数组中维护电子邮件结尾。因此,对数组进行迭代非常重要。@k0pernikus我知道,但你不能这样做,SQL不会识别syntaxI中的类似内容,因为每次添加新的电子邮件结尾时,都要避免维护select查询。我想在配置数组中维护电子邮件结尾。因此,对数组进行迭代非常重要。@k0pernikus我知道,但你不能这样做,SQL在syntax@PawełKolanowski你能详细说明一下吗?什么更好?对不起,我错了。乍一看,我认为解决这个问题可能更好,但似乎这个解决方案会有效。@PawełKolanowski你能详细说明一下吗?什么更好?对不起,我错了。乍一看,我认为解决这个问题可能更好,但似乎这个解决方案会有效。