我如何在学说和SELECT AS中正确使用IF语句?

问题描述 投票:0回答:1
$query = Doctrine_Query::create()
->select('IF(m.sender_id = ?, m.receiver_id, m.sender_id) AS uid', $my_id)
->from('Message m')
->where('m.receiver_id = ? OR m.sender_id = ?', array($my_id,$my_id))
->groupBy('uid')

我无法按'uid'分组,好像AS uid根本不起作用...

如果我尝试通过-> getSqlQuery()检索生成的sql并直接在Mysql中尝试,它将起作用。

SELECT IF(s.sender_id = 1, s.receiver_id, s.sender_id) AS s__0 
FROM message s 
WHERE (s.receiver_id = 1 OR s.sender_id = 1) GROUP BY s__0
doctrine
1个回答
0
投票
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$q = $qb
      ->select("c.id")
      ->addSelect("CASE WHEN (c.parent IS NULL) THEN c.name ELSE 'something' END")
      ->from("MyBundle:Category", "c")
      ->leftJoin("c.parent", "t");

echo $q->getQuery()->getSQL();
© www.soinside.com 2019 - 2024. All rights reserved.