【问题标题】:SQL Multiple sorting and groupingSQL 多重排序和分组
【发布时间】:2012-05-31 01:18:00
【问题描述】:

编辑:我正在使用 DQL

我正在寻求有关 SQL 查询的帮助。

我的表有一个电影列表,每个电影都有一个标题、seriesName 和 seriesNumber。是否可以对它们进行排序,使标题按 A-Z 列出,但是当一个系列出现时,该系列被分组在一起,系列名称按字母顺序排列,就好像它在电影标题列中一样,系列中的条目按系列编号排序。

不好的解释,但基本上我想要的是这样的:

MovieTitle                  SeriesName          SeriesNumber
Blade Runner                NULL                NULL
District 9                  NULL                NULL
Hot Fuzz                    NULL                NULL
I am Legend                 NULL                NULL
Fellowship of the Ring      Lord of the Rings   1
Two Towers, The             Lord of the Rings   2
Return of the King          Lord of the Rings   3
Lost in Translation         NULL                NULL
Matrix, The                 Matrix              1
Matrix Reloaded, The        Matrix              2
Matrix Revolutions, The     Matrix              3
Requiem for a Dream         NULL                NULL
This is Spinal Tap          NULL                NULL
Zodiac                      NULL                NULL

提前致谢。

【问题讨论】:

  • DQL,在 Symfony2 中做一些事情

标签: sql doctrine dql


【解决方案1】:
SELECT * FROM x
ORDER BY CASE 
   WHEN SeriesName is NOT NULL THEN SeriesName ELSE MovieTitle  END
   , SeriesNumber

你可能不得不这样做

SELECT * FROM(
  SELECT *, CASE WHEN SeriesName is NOT NULL THEN SeriesName ELSE MovieTitle END SortOrder  FROM x
)
ORDER BY SortOrder,SeriesNumber

【讨论】:

  • 非常感谢,我尝试让它在直接 DQL 中工作,但有点麻烦,最终使用原始 sql。感谢帮助:)
猜你喜欢
  • 2020-04-16
  • 2018-01-29
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
相关资源
最近更新 更多