如何将记录字段添加到已排序的SQL结果中?

时间:2017-06-21 12:53:56

标签: mysql sql sorting rank

我试图将记录号字段添加到已排序的SQL结果表中,但我没有得到我之后的结果。

这里的结果表按id正确排序但没有记录号字段:

SELECT id, SeriesName
  FROM tvseries 
 WHERE SeriesName LIKE '%certain-tv-show%'
 ORDER BY id ASC;

+--------+------------+
| id     | SeriesName |
+--------+------------+
|  77092 | Series1    |
|  79395 | Series2    |
|  79949 | Series3    |
|  80341 | Series4    |
| 203581 | Series5    |
| 242521 | Series6    |
| 250374 | Series7    |
| 252679 | Series8    |
| 269228 | Series9    |
| 271452 | Series10   |
| 274997 | Series11   |
| 292986 | Series12   |
| 293986 | Series13   |
| 307475 | Series14   |
| 319215 | Series15   |
+--------+------------+

下面是尝试将记录编号字段添加到与上面相同的结果表中。但是,ORDER BY id ASC子句似乎被忽略了,因为结果类似于我没有订购结果时得到的结果(未显示在任何地方,因为它会在禁止记录号字段的情况下重复):

SELECT @rownum:=@rownum+1 as Num, Results.* 
  FROM (SELECT @rownum:=0) r, 
       (SELECT id, SeriesName from tvseries 
         WHERE SeriesName LIKE '%certain-tv-show%'
         ORDER BY id ASC) Results;

+------+--------+------------+
| Num  | id     | SeriesName |
+------+--------+------------+
|    1 |  77092 | Series1    |
|    2 |  79395 | Series2    |
|    3 |  79949 | Series3    |
|    4 |  80341 | Series4    |
|    5 | 319215 | Series15   |
|    6 | 203581 | Series5    |
|    7 | 242521 | Series6    |
|    8 | 250374 | Series7    |
|    9 | 252679 | Series8    |
|   10 | 269228 | Series9    |
|   11 | 271452 | Series10   |
|   12 | 274997 | Series11   |
|   13 | 307475 | Series14   |
|   14 | 292986 | Series12   |
|   15 | 293986 | Series13   |
+------+--------+------------+

" DOCTORED"下面的结果是我希望在MySQL中实现的目标:

+------+--------+------------+ | Num | id | SeriesName | +------+--------+------------+ | 1 | 77092 | Series1 | | 2 | 79395 | Series2 | | 3 | 79949 | Series3 | | 4 | 80341 | Series4 | | 5 | 203581 | Series5 | | 6 | 242521 | Series6 | | 7 | 250374 | Series7 | | 8 | 252679 | Series8 | | 9 | 269228 | Series9 | | 10 | 271452 | Series10 | | 11 | 274997 | Series11 | | 12 | 292986 | Series12 | | 13 | 293986 | Series13 | | 14 | 307475 | Series14 | | 15 | 319215 | Series15 | +------+--------+------------+

我在MariaDB 10.0.29中尝试这一点,这使我认为在任何版本的MariaDB或MySQL中都不可能。一位同事建议我查看存储过程,但我是一个业余爱好者DBA,这将超出我的范围。

编辑:

@GordonLinoff:谢谢你的回答。如果我需要将您的解决方案用于更复杂的查询,我将如何解决它:

SELECT sea.season as Season, ep.EpisodeNumber as Episode, ep.EpisodeName as Title
FROM tvseasons sea
INNER JOIN tvepisodes ep on ep.seasonid = sea.id
INNER JOIN tvseries ser on ser.id = ep.seriesid
WHERE ser.id = 'some_id' AND Season != 0
ORDER BY Season,Episode ASC;

EDIT2: 我已按如下方式修改了我的查询:

SELECT ($rn := $rn + 1) AS Num, sea.season AS Season, ep.EpisodeNumber AS Episode, ep.EpisodeName AS Title
  FROM tvseasons sea
 INNER JOIN tvepisodes ep on ep.seasonid = sea.id
 INNER JOIN tvseries ser on ser.id = ep.seriesid
 CROSS JOIN (SELECT $rn := 0) params 
 WHERE ser.id = 'some_id' AND Season != 0
 ORDER BY Season,Episode ASC;

但是我收到以下错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':= $rn + 1) AS Num, sea.season AS Season, ep.EpisodeNumber AS Episode, ep.Episod' at line 1

无知的想法?

EDIT3: 另外,我相信CROSS JOIN实际上是有所作为的。要在完整的背景下查看(希望我可以发布链接):Querying The TVDB

EDIT4: 我没有意识到我已经为@换了$ - 感谢抬头 - 现在全部排序了。

1 个答案:

答案 0 :(得分:0)

这样做你想要的吗?

SELECT (@rn := @rn + 1) as num, t.id, t.SeriesName
FROM tvseries t CROSS JOIN
     (SELECT @rn := 0) params
WHERE t.SeriesName LIKE '%certain-tv-show%'
ORDER BY t.id ASC;

我认为你的查询也应该有用。