symfony2 原则选择 IFNULL [英] symfony2 doctrine select IFNULL

查看:20
本文介绍了symfony2 原则选择 IFNULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有这个代码:

SELECT 
IFNULL(s2.id,s1.id) AS effectiveID, 
IFNULL(s2.status, s1.status) AS effectiveStatus, 
IFNULL(s2.user_id, s1.user_id) as effectiveUser,
IFNULL(s2.likes_count, s1.likes_count) as effectiveLikesCount

FROM statuses AS s1
LEFT JOIN statuses AS s2 ON s2.id = s1.shared_from_id
WHERE s1.user_id = 4310
ORDER BY effectiveID DESC
LIMIT 15

我需要将其重写为 querybuilder.类似的东西?

And i need to rewrite it to querybuilder. Something like that?

        $fields = array('IFNULL(s2.id,s1.id) AS effectiveID','IFNULL(s2.status, s1.status) AS effectiveStatus', 'IFNULL(s2.user_id, s1.user_id) as effectiveUser','IFNULL(s2.likes_count, s1.likes_count) as effectiveLikesCount');

    $qb=$this->_em->createQueryBuilder()
             ->select($fields)
             ->from('WallBundle:Status','s1')
             ->addSelect('u')
             ->where('s1.user = :user')
             ->andWhere('s1.admin_status = false')
             ->andWhere('s1.typ_statusu != :group')
             ->setParameter('user', $user)
             ->setParameter('group', 'group')
             ->leftJoin('WallBundle:Status','s2', 'WITH', 's2.id=s1.shared_from_id')  
             ->innerJoin('s1.user', 'u')               
             ->orderBy('s1.time', 'DESC')
             ->setMaxResults(15);

     var_dump($query=$qb->getQuery()->getResult());die();

这个错误是

[Syntax Error] line 0, col 7: Error: Expected known function, got 'IFNULL'

推荐答案

像这样使用 COALESCE 而不是 IFNULL

Use COALESCE instead of IFNULL like this

$fields = array('COALESCE(s2.id,s1.id) AS effectiveID','COALESCE(s2.status, s1.status) AS effectiveStatus', 'COALESCE(s2.user_id, s1.user_id) as effectiveUser','COALESCE(s2.likes_count, s1.likes_count) as effectiveLikesCount');

COALESCE 返回列表中第一个不为空的值,因此如果 A 为空且 B 不为空,则 COALESCE(A,B) 将返回 B.

COALESCE return the first value not null in the list, so if A is null and B not null, then COALESCE(A,B) will return B.

这篇关于symfony2 原则选择 IFNULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆