計數空值我有這樣一個查詢:主義不是比較
$thism = new \DateTime('first day of this month midnight');
$parameters = array('thism' => $thism, 'user' => $user,'status'=>1);
return $this->createQueryBuilder('q')
->select('count(q)')
->where('q.createdAt > :thism')
->andWhere('q.user = :user')
->andWhere('q.completed != :status')
->setParameters($parameters)
->getQuery()
->getSingleScalarResult();
這應該算從本月起我所有的不完整的對象。問題出在q.completed != :status
。這不會計算具有completed=NULL
的對象。完成是一個布爾值
class Quote
{
/**
* @var boolean
*
* @ORM\Column(name="completed", type="boolean", nullable=true)
*/
private $completed;
}
難道我做錯了什麼,或者爲什麼不主義算NULL作爲比真有什麼不同?
我試過用1,true,TRUE
替換'status'=>1
,它們都帶有和沒有引號,仍然沒有效果。
格式化的查詢:
SELECT count(q0_.id) AS sclr_0 FROM quote q0_ WHERE q0_.created_at > ? AND q0_.user_id = ? AND q0_.completed <> ?
Parameters: ['2016-04-01 00:00:00', '1', 1]
SELECT count(q0_.id) AS sclr_0 FROM quote q0_ WHERE q0_.created_at > '2016-04-01 00:00:00' AND q0_.user_id = '1' AND q0_.completed <> 1;
生成的SQL看起來像什麼? (你可以在日誌中看到它) – Miro
@Miro將它添加到問題中。 –