Simple IF test statement in Doctrine(Doctrine 中的简单 IF 测试语句)
问题描述
Doctrine 是否支持 IF 语句?我收到以下错误:
Does Doctrine support IF statements? I get the following error:
Expected known function, got 'IF'
使用 IF 执行此查询时:
while executing this query with IF:
$qb->select("c.id, IF(c.type_id LIKE 9, c.name, c.lastname) as name")
用纯 SQL 重写时它工作正常.有什么解决方法吗?
It works fine while re-written in pure SQL. Any workarounds?
推荐答案
是的 if
中的语句不支持你可以将它转换为 case when
Yes if
statements in doctrine is not supported you may convert it to 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
答案是肯定的,非常允许.这是一个例子
The answer is yes very much allowed. Here is an example
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 测试语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine 中的简单 IF 测试语句


基础教程推荐
- 带有WHERE子句的LAG()函数 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01