SQL多重排序和分组

时间:2012-05-31 01:18:00

标签: sql doctrine dql

编辑:我正在使用DQL

我正在寻找SQL查询的帮助。

我的表中有一个电影列表,每个电影都有一个标题,seriesName和seriesNumber。是否可以对它们进行排序以使标题列为AZ,但是当系列出现时,该系列被组合在一起,其中seriesName按字母顺序排列,就像它在movieTitle列中一样,并且系列中的条目按seriesNumber排序。 / p>

错误的解释,但基本上我想要的是:

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

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