【问题标题】:Add a select based on a conditional to doctrine query builder添加基于条件的选择到学说查询构建器
【发布时间】:2018-07-17 15:41:32
【问题描述】:

如何将基于条件的选择添加到理论查询构建器?

我想复制类似这样的 SQL:

select p.id, p.id = 3 as first_result
from problem p
order by first_result desc, p.id

【问题讨论】:

  • 是否需要在选择部分单独列,或者您只需要根据p.id = 3 对结果进行排序?
  • 不需要单独的选择结果——我在 SQL 中就是这样做的

标签: doctrine doctrine-query


【解决方案1】:

根据文档,您可以使用表达式并将此列标记为HIDDEN(不包括在结果中),但可以在查询中使用,就像您的情况一样订购结果

DQL 看起来像

SELECT p, p.id = 3 AS HIDDEN first_result
FROM YourProblemEntity p 
ORDER BY first_result DESC, p.id

或者你可以引入CASE表达式

SELECT p, 
CASE WHEN p.id = 3 THEN 1 ELSE 0 END AS HIDDEN first_result
FROM YourProblemEntity p 
ORDER BY first_result DESC, p.id

See DQL SELECT Examples

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多