2017-08-08 94 views
1

我有一个MySQL日期存储在DATETIME格式。所以我想知道如何在我的Doctrine QueryBuilder的where子句中使用date()。例如,2013-02-01 12:51:17是MySQL中的日期。但我只需要检索日期。这是我曾尝试:你的where子句中在Doctrine QueryBuilder中的MySQL DATETIME的Retreive date

QueryException: [Syntax Error]: Error: Expected Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS, got 'date'

+0

https://stackoverflow.com/questions/25883616/how-can-i-use-date-in-doctrine-2-dql – Cerad

回答

-1

你不需要的“日期”:

$qb = $this->getEntityManager()->createQueryBuilder() 
    ->select('t.balance','a.id','t.date') 
    ->from('TestMainBundle:Transaction','t')    
    ->groupBy('a.id') 
    ->orderBy('a.id') 
    ->where("t.date in date('t.date') "); 

return $qb->getQuery()->getResult(); 

我收到以下错误。

中庸之道删除它是这样的:

->where('t.date in (:yourwanteddate)') 
->setParameter('yourwanteddate', '2013-02-01 12:51:17'); 
+1

有日期之间的微妙(但通常显著)差约会时间。 – Cerad

+0

但我需要基于日期时间从datetime中检索查询。在mysql中它是以日期时间格式..我需要从这个日期时间retreive日期。例如: - >和Where(date('t.date')in (:yourwanteddate)') - > setParameter('yourwanteddate','2013-02-01'); – Programmer

0

嗨,您可以使用SUBSTRING解决您的probleme

->where('SUBSTRING(t.date, 1, 10) IN (:param)') 
->setParameter('param', array('2017-04-06')) 
1

正如在评论中指出,你不能使用特定的MySQL date()date_format()在Doctrine中起作用。

但是对于搜索的日期时间字段在特定日期的特定情况下,你可以像对待一个字符串的日期,从而利用运营商作为

But I need to retreive only the date

LIKE

->Where('t.date LIKE :date') 
->setParameter(':date', "$date%") 

你只需使用format("Y-m-d")方法格式化返回的值。即

echo $row->getDate()->format("Y-m-d"); 
相关问题