我正在尝试计算列字段包含特定字符串的记录。
这是我的查询:
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQueryBuilder()
->select("count(e.$column)")
->from("AppBundle:$table", "e")
->where("e.urlid LIKE \"-test\"")
->getQuery();
$occurences = $query->getSingleScalarResult();
当我在 mysql 上运行手动查询时它有效,所以它可能是一个教义。我从学说中得到这个错误:
[Syntax Error] line 0, col 66: Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got '"'
如有任何帮助,我们将不胜感激!
最佳答案
尝试用这样的东西来正确设置 like 表达式:
$qb->andWhere($qb->expr()->like('user.name', $qb->expr()->literal('%'.$name.'%')));
在你的情况下试试这个:
$key = '-test';
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$query = $qb
->select("count(e.$column)")
->from("AppBundle:$table", "e");
$qb->andWhere($qb->expr()->like('e.urlid', $qb->expr()->literal('%'.$key.'%')));
$occurences = $qb->getQuery()->getSingleScalarResult();
希望对你有帮助
关于mysql - 计算满足条件的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37022898/