2017-06-21 63 views
1

我想添加一個記錄號字段到排序的SQL結果表,但我沒有得到我後的結果。如何將記錄字段添加到排序後的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:[email protected]+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 | 
+------+--------+------------+ 

的「篡改「的結果是什麼,我希望從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 

想法爲無知?

編輯3: 另外,我相信CROSS JOIN實際上是有什麼不同。要看到它在完整的上下文(希望我允許張貼鏈接):Querying The TVDB

EDIT4: 我沒有意識到我已經換出@$ - 感謝擡起頭 - 現在所有排序。

回答

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; 

我確實認爲你的查詢也應該有效。

+0

是的,這個按預期工作;不知道爲什麼我的查詢不起作用。我真的想擴大我對SQL查詢的知識,因爲我不知道'cross join'做了什麼,但它顯然解決了我的問題。預訂建議?只要我驗證它適用於大量結果集的幾個查詢,就會將其標記爲答案。 – AnthonyK

+0

@AnthonyK。 。 。我不認爲'CROSS JOIN'解決了你的問題。在這種情況下,它與逗號相同。我只是在'FROM'子句中隱藏了逗號,並且我明確指出'JOIN'類型。 –

+0

請同時參閱對我的問題的編輯。我需要測試更大的結果集,但不知道如何將「交叉連接」縫合到我的「內部連接」。 – AnthonyK

相關問題