2017-08-08 54 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"); 
相關問題