mysql - 在 OrderBy Doctrine Querybuilder 中使用 IS NULL 和 COALESCE

标签 mysql symfony doctrine-orm doctrine sql-order-by

我基本上有以下 (My)SQL 查询

SELECT * FROM `address`
ORDER BY ISNULL(`company`), `company` ASC, COALESCE(`parent_id`, `address_id`), `parent_id` IS NOT NULL

哪个工作最完美

我的目标是以下排序输出

ID| PARENT_ID | COMPANY | NAME
1 | NULL      | A       | NULL
2 | 1         | A.A     | NULL
3 | 1         | A.B     | NULL
4 | NULL      | B       | NULL
5 | NULL      | C       | NULL
6 | NULL      | D       | NULL
7 | 6         | D.A     | NULL
8 | NULL      | NULL    | A

我正在使用 Symfony 2 和 Doctrine。 目前我使用 Doctrine query Builder 因为 OrderBy 应该是我用不同方面实现的搜索的一部分(按标签搜索,按不同字段“地址”搜索......)

当我尝试将“IS NULL”或“COALESCE”添加到

$qb->add('orderBy', ' >ORDERBY_STATEMENT< ');

或者以任何其他方式使用查询构建器编写 order by 语句

我得到以下信息

[Syntax Error] line 0, col 90: Error: Expected end of string, got 'IS'

我发现 Doctrine 不是特定于供应商的,因此不能支持所有供应商功能。

我读了一篇关于扩展 Doctrines DQL 函数的文章。但是已经有一个使用 Doctrine Expressions 的“isNull”函数,它对我来说在 OrderBy 中不起作用。

有人知道如何使用查询生成器实现所描述的 OrderBy 语句吗?

最佳答案

可能这是可以帮助的:

$qb = $em->createQueryBuilder();
$qb->select('Entity, COALESCE(Entity.column1, Entity.column2) as columnOrder')
   ->from('Namespace\EntityName', 'Entity')
   ->addOrderBy('columnOrder', 'ASC')
   ->getQuery()
   ->execute();

关于mysql - 在 OrderBy Doctrine Querybuilder 中使用 IS NULL 和 COALESCE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18148818/

相关文章:

mysql - 连接 3 个相关表 mySql

mysql - Doctrine2 中从 n+1 到 n-1 的订购日期

php - Symfony 4 : DependencyInjection Component - Uncaught ReflectionException: Class does not exist

php - 如何将 "distinct on"与学说一起使用?

mysql - 如何在学说中左连接子选择

php - 原则 2 DQL - 在 QueryBuilder 中嵌套表达式和函数

mysql - 使用 Rails 从组数组中检索拥有所有组的用户

php - PDF 可下载 PHP

php - 需要重新认证 : INSERT base64 to BLOB in mySQL database

symfony - 渲染过程中抛出异常?