原则子字符串字段,其中子句中带有别名



我有以下查询:

$query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country)
->from("AppBundleEntityImage" ,"p")
->where("p.aktuellste = 1")
->andWhere($qb->expr()->in('country',':country'))
->setParameter(':country','de')
->orderBy('country','DESC')
->getQuery();

例如,我想选择substring(1,2)网站de的所有行。但countryWHERE条款中教义的未知列。引发以下异常:

SQLSTATE[42S22]:找不到列:1054 未知列"sclr_0" 'where 条款

有趣的是,Doctrine知道ORDER BY条款中的country列。

有人知道如何解决这个问题吗?

如果要搜索别名列,则需要使用HAVING,因为它位于计算字段上。 将其放在WHERE子句中是假设您的Image实体中有一个名为country的成员,而事实并非如此。

$query = $qb
->select($qb->expr()->substring('p.website', 1, 5).'AS country')
->from('AppBundleEntityImage', 'p')
->andWhere('p.aktuellste = 1')
->andHaving($qb->expr()->in('country', ':country'))
->setParameter(':country', 'de')
->orderBy('country', 'DESC')
->getQuery()
;

有关更多信息,请参阅 MySQL 处理分组依据:

MySQL 扩展允许在 HAVING 子句中使用别名 对于聚合列

查询中存在一些语法错误,但不确定这是否解决了您的问题。 请尝试以下操作:

$query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country')
->from("AppBundleEntityImage" ,"p")
->where("p.aktuellste = 1")
->andWhere($qb->expr()->in('country=:country'))
->setParameter('country','de')
->orderBy('country','DESC')
->getQuery();

最新更新