mysql - Doctrine 中的简单 IF 测试语句

标签 mysql doctrine

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 Doctrine 中的陈述不被支持,您可以将其转换为 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)

关于mysql - Doctrine 中的简单 IF 测试语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29792968/

相关文章:

php - 如何使用 symfony 获取 Doctrine 实体持久集合数据值

javascript - Kendo UI 中添加记录功能,始终插入两行

php - 在 PHP 中为什么它只回显最后一项而且我知道在数组中它不止一个

mysql - 从 VM 计算引擎连接到我的数据库

java - javax.persistence 中的 ManyToMany 允许重复条目

php - 使用php文件上传错误

exception - 跳过异常(exception) Doctrine 迁移

orm - 具有该实体外键的 Doctrine 实体

php - 鉴别器图中缺少的实体

php - Doctrine DQL、类表继承和访问子类字段