原则中的简单IF测试语句 [英] Simple IF test statement in Doctrine

查看:50
本文介绍了原则中的简单IF测试语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学说是否支持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

更新: 根据评论,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)

这篇关于原则中的简单IF测试语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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