[英]Syntax Error: line 0, col 152: Error: Expected =, <, <=, <>, >, >=, !=, got 'BETWEEN'
When I try the following code:当我尝试以下代码时:
return $this->createQueryBuilder('e')
->select('e.user_id', 'e.year', 'e.week_number', 'e.approved_by')
->where('e.user_id = :userID')
->andWhere('(e.year * 100 + e.week_number) BETWEEN :startDate and :endDate')
->setParameter('userID', $userID)
->setParameter('startDate', ($startYear * 100 + $startWeek))
->setParameter('endDate', ($endYear * 100 + $endWeek))
->getQuery()
->getResult()
;
I get the error [Syntax Error] line 0, col 149: Error: Expected =, <, <=, <>, >, >=, ,=, got 'BETWEEN'
.我收到错误[Syntax Error] line 0, col 149: Error: Expected =, <, <=, <>, >, >=, ,=, got 'BETWEEN'
。
If I print my query, it looks like this:如果我打印我的查询,它看起来像这样:
dql: "SELECT e.user_id, e.year, e.week_number, e.approved_by FROM App\Entity\... e WHERE e.user_id = :userID AND ((e.year * 100 + e.week_number) BETWEEN :startDate and :endDate)"
If I use this query in SSMS and replace the variables with the actual data.如果我在 SSMS 中使用此查询并将变量替换为实际数据。 It all works perfectly fine.这一切都很好。
My variables are have the following value:我的变量具有以下值:
$startYear = 2021;
$startWeek = 52;
$endYear = 2022;
$endWeek = 05;
// :startDate = 202152;
// :endDate = 202205;
The calculations probably break the query builder.计算可能会破坏查询生成器。 Mostly because I do similar query's: ->andWhere('e.week_number between:startWeek and:endWeek')
that do indeed work.主要是因为我做了类似的查询: ->andWhere('e.week_number between:startWeek and:endWeek')
确实有效。 I don't know how to fix them or why this happens.我不知道如何解决它们或为什么会发生这种情况。
Im using Symfony 4.4.我正在使用 Symfony 4.4。 SQL Server for my database. SQL 服务器用于我的数据库。
This comment of Zhorov fixed my problem. Zhorov 的这个评论解决了我的问题。
You may try to change the
BETWEEN
condition with->andWhere('(e.year * 100 + e.week_number) >=:startDate')
and->andWhere('(e.year * 100 + e.week_number) <=:endDate')
您可以尝试使用->andWhere('(e.year * 100 + e.week_number) >=:startDate')
和->andWhere('(e.year * 100 + e.week_number) <=:endDate')
更改BETWEEN
条件->andWhere('(e.year * 100 + e.week_number) <=:endDate')
Still quite weird the query builder can't handle it.查询生成器无法处理它仍然很奇怪。 I mean not much has changed and SQL Server itself finds it all good.我的意思是没有太大变化,SQL 服务器本身觉得一切都很好。 If someone can explain how this works, I probably vote it as an answer.如果有人可以解释这是如何工作的,我可能会投票作为答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.