加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql – Doctrine 2 DQL CONCAT字段和常量字符串

发布时间:2020-12-11 23:53:39 所属栏目:MySql教程 来源:网络整理
导读:我在MySQL表中有字段firstname和lastname.为方便起见,我想在我的Doctrine 2实体中添加一个名为full_name的计算列.在普通的MySQL中我会做这样的事情 SELECT CONCAT(firstname," ",lastname) AS full_name FROM customers; 但是,连接字段和常量字符串(在这种情

我在MySQL表中有字段firstname和lastname.为方便起见,我想在我的Doctrine 2实体中添加一个名为full_name的计算列.在普通的MySQL中我会做这样的事情

SELECT CONCAT(firstname," ",lastname) AS full_name FROM customers;

但是,连接字段和常量字符串(在这种情况下为“”)似乎不适用于Doctrine的CONCAT实现.使用以下代码时

$repository
    ->createQueryBuilder('customer')
    ->select('CONCAT(customer.firstname,customer.lastname) AS full_name')
    // ...

我收到了错误

[Syntax Error] line 0,col 91: Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression,got '"'

我怎样才能实现与MySQL相同的行为? 最佳答案 显然,DQL中的字符串只能用单引号封装,而不能用双引号封装.文档中的简短搜索并未直接提及此行为,但我注意到所有包含常量字符串的示例都使用单引号.

更改

->select('CONCAT(customer.firstname,customer.lastname) AS full_name')

->select('CONCAT(customer.firstname,' ',customer.lastname) AS full_name')

要么

->select("CONCAT(customer.firstname,' ',customer.lastname) AS full_name")

解决了这个问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读