Doctrine 中的简单 IF 测试语句
本教程将介绍Doctrine 中的简单 IF 测试语句的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
Doctrine 是否支持 IF 语句?我收到以下错误:
Expected known function, got 'IF'
使用 IF 执行此查询时:
$qb->select("c.id, IF(c.type_id LIKE 9, c.name, c.lastname) as name")
用纯 SQL 重写时它工作正常.有什么解决方法吗?
推荐答案
是的 if
中的语句不支持你可以将它转换为 case when
IF(c.type_id LIKE 9, c.name, c.lastname) as name
到
case when c.type_id = 9 then c.name else c.lastname end as name
更新:从评论中,concat
函数在 case-when
答案是肯定的,非常允许.这是一个例子
mysql> select * from timesheets ;
+-----------+-------+----------+
| client_id | hours | category |
+-----------+-------+----------+
|1 | 1.50 | onsite|
|1 | 1.50 | onsite|
|1 | 1.00 | remote|
|2 | 1.50 | remote|
+-----------+-------+----------+
4 rows in set (0.00 sec)
mysql> select
case when category = 'onsite' then concat('ON',' ',hours) else hours
end as dd from timesheets ;
+---------+
| dd|
+---------+
| ON 1.50 |
| ON 1.50 |
| 1.00 |
| 1.50 |
+---------+
4 rows in set (0.00 sec)
好了关于Doctrine 中的简单 IF 测试语句的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。